MAD Practical 15
MAD Practical 15
<Button
android:id="@+id/simple_toast_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="20dp"
android:text="Simple Toast"/>
<Button
android:id="@+id/custom_toast_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="20dp"
android:text="Custom Toast"/>
</LinearLayout>
custom_toast.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"
android:background="@drawable/ic_launcher_background"/>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
Java Code:
package com.example.madprac15;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button simpleToast = (Button) findViewById(R.id.simple_toast_button);
Button customToast = (Button) findViewById((R.id.custom_toast_button));
simpleToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "This is a simple toast.",
Toast.LENGTH_SHORT).show();
}
});
customToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup)
findViewById(R.id.custom_toast_layout));
TextView toastText = (TextView) findViewById(R.id.toast_text);
toastText.setText("This is a custom toast");
Toast customToast = new Toast(getApplicationContext());
customToast.setDuration(Toast.LENGTH_LONG);
customToast.setView(layout);
customToast.show();
}
});
}
}
Output:
Q2. Write a program to display three checkboxes and one button named “Order” as
shown below. Once you click on button it should display simple toast along with items
name and total price. Display custom toast on selection of checkbox.
XML Code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="10dp"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/pizza_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza"
android:onClick="showMessage"
android:textSize="30sp"/>
<CheckBox
android:id="@+id/coffee_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coffee"
android:onClick="showMessage"
android:textSize="30sp"/>
<CheckBox
android:id="@+id/burger_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Burger"
android:onClick="showMessage"
android:textSize="30sp"/>
<Button
android:id="@+id/order_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:textSize="30sp"
android:layout_marginTop="200dp"/>
</LinearLayout>
custom_toast.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"
android:background="@drawable/ic_launcher_background"/>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
Java Code:
package com.example.madprac15;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
orderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String result = "You have ordered:\n";
int price = 0;
if(pizzaCb.isChecked() || burgerCb.isChecked() || coffeeCb.isChecked()) {
result += pizzaCb.isChecked() ? "Pizza\n" : "";
result += coffeeCb.isChecked() ? "Coffee\n" : "";
result += burgerCb.isChecked() ? "Burger\n" : "";
price += pizzaCb.isChecked() ? 100 : 0;
price += coffeeCb.isChecked() ? 45 : 0;
price += burgerCb.isChecked() ? 75 : 0;
result += "Total price: " + price;
} else {
result = "Nothing selected";
}
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
});
}
Output: