0% found this document useful (0 votes)
1K views7 pages

MAD Practical 15

The document describes a program with the following functionality: 1. It displays three checkboxes (Pizza, Coffee, Burger) and a button labeled "Order" on the main Android activity layout. 2. When the user clicks the "Order" button, it displays a simple toast listing the items ordered and their total price. 3. When the user selects a checkbox, it displays a custom toast containing an image and the name of the selected item.

Uploaded by

daisukekambe543
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)
1K views7 pages

MAD Practical 15

The document describes a program with the following functionality: 1. It displays three checkboxes (Pizza, Coffee, Burger) and a button labeled "Order" on the main Android activity layout. 2. When the user clicks the "Order" button, it displays a simple toast listing the items ordered and their total price. 3. When the user selects a checkbox, it displays a custom toast containing an image and the name of the selected item.

Uploaded by

daisukekambe543
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

MAD Practical 15

Hassaan Shaikh – 210429


Q1. Write a Program to create 2 buttons as “Simple Toast” and “Custom Toast” on
click of Simple Toast button normal toast must be displayed and on click of Custom
Toast button a message with an image must be displayed.
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">

<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;

public class MainActivity extends AppCompatActivity {

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

Button orderButton = (Button) findViewById(R.id.order_button);


CheckBox pizzaCb = (CheckBox) findViewById(R.id.pizza_box);
CheckBox coffeeCb = (CheckBox) findViewById(R.id.coffee_box);
CheckBox burgerCb = (CheckBox) findViewById(R.id.burger_box);

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();
}
});
}

public void showMessage(View view) {


if(((CheckBox) view).isChecked()) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup)
findViewById(R.id.custom_toast_layout));
TextView toastText = layout.findViewById(R.id.toast_text);
toastText.setText(((CheckBox) view).getText().toString() + " selected.");

Toast toast = new Toast(getApplicationContext());


toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
}
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy