0% found this document useful (0 votes)
7 views7 pages

Ex 15

The document contains an Android application code that demonstrates the creation of simple and custom Toast messages using buttons. It includes XML layout files for the main activity and custom Toast layout, as well as Java code for handling button clicks to display Toast messages. The application allows users to select items with checkboxes and shows the total price in a custom Toast when the submit button is clicked.

Uploaded by

Ravi Pardhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Ex 15

The document contains an Android application code that demonstrates the creation of simple and custom Toast messages using buttons. It includes XML layout files for the main activity and custom Toast layout, as well as Java code for handling button clicks to display Toast messages. The application allows users to select items with checkboxes and shows the total price in a custom Toast when the submit button is clicked.

Uploaded by

Ravi Pardhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

<RelativeLayout xmlns:android="http://schemas.android.

com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity">
<!-- Button's for simple and custom Toast -->
<Button
android:id="@+id/simpleToast"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#f00"
android:text="Simple Toast"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:id="@+id/customToast"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/simpleToast"
android:layout_centerHorizontal="true"
android:layout_margin="50dp"
android:background="#0f0"
android:text="Custom Toast"
android:textColor="#fff"
android:textSize="20sp" />

</RelativeLayout>

package com.example.helloworld;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

Button simpleToast, customToast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the reference of Button's
simpleToast = (Button) findViewById(R.id.simpleToast);
customToast = (Button) findViewById(R.id.customToast);
// perform setOnClickListener event on simple Toast Button
simpleToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// initiate a Toast with message and duration
Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast In
Android", Toast.LENGTH_LONG); // initiate the Toast with context, message and duration
for the Toast
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
// set gravity for the Toast.
toast.show(); // display the Toast

}
});
// perform setOnClickListener event on custom Toast Button
customToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Retrieve the Layout Inflater and inflate the layout from xml
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.activity_toast_custom_view,
(ViewGroup) findViewById(R.id.toast_layout_root));
// get the reference of TextView and ImageVIew from inflated layout
TextView toastTextView = (TextView)
layout.findViewById(R.id.toastTextView);
ImageView toastImageView = (ImageView)
layout.findViewById(R.id.toastImageView);
// set the text in the TextView
toastTextView.setText("Custom Toast In Android");
// set the Image in the ImageView

// create a new Toast using context


Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG); // set the duration for the
Toast
toast.setView(layout); // set the inflated layout
toast.show(); // display the custom Toast

}
});
}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/custom_toast_background"
android:orientation="horizontal"
android:padding="8dp"
>
<!-- ImageVView and TextView for custom Toast -->
<ImageView
android:id="@+id/toastImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp" />

<TextView
android:id="@+id/toastTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<solid android:color="@android:color/holo_green_light" />

<corners android:radius="15dp" />

</shape>
package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

CheckBox cb1,cb2,cb3;
Button b1 ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cb1=(CheckBox)findViewById(R.id.cbi1);
cb2=(CheckBox)findViewById(R.id.cbi2);
cb3=(CheckBox)findViewById(R.id.cbi3);
b1=(Button)findViewById(R.id.bi1);

b1.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {


int sum=0;
StringBuffer r=new StringBuffer();

if(cb1.isChecked()) {
r.append("\n").append(cb1.getText()).append("150Rs");
sum += 150;
}

if(cb2.isChecked()) {
r.append("\n").append(cb2.getText()).append("50Rs");
sum += 50;
}

if(cb3.isChecked()) {
r.append("\n").append(cb3.getText()).append("100Rs");
sum += 100;
}

r.append("\nTotal:").append(sum);

LayoutInflater inflater = getLayoutInflater();


View layout = inflater.inflate(R.layout.activity_toast_custom_view,
(ViewGroup) findViewById(R.id.custom));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG); // set the duration for the
Toast
toast.setView(layout);
TextView toastTextView = (TextView)
layout.findViewById(R.id.toastTextView);

toastTextView.setText(r.toString());

toast.show();

}
});

}
}

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/linear"
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">

<CheckBox
android:id="@+id/cbi1"
android:layout_width="match_parent"
android:layout_height="77dp"
android:text="Pizza" />

<CheckBox
android:id="@+id/cbi2"
android:layout_width="match_parent"
android:layout_height="82dp"
android:text="Coffee" />

<CheckBox
android:id="@+id/cbi3"
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="Burger" />

<Button
android:id="@+id/bi1"
android:layout_width="match_parent"
android:layout_height="196dp"
android:text="Submit" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/custom_toast_background"
android:orientation="horizontal"
android:padding="8dp"
>
<!-- ImageVView and TextView for custom Toast -->

<ImageView
android:id="@+id/toastImageView"
android:layout_width="110dp"
android:layout_height="83dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/download" />

<TextView
android:id="@+id/toastTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<solid android:color="@android:color/holo_green_light" />

<corners android:radius="15dp" />

</shape>

You might also like