0% found this document useful (0 votes)
5 views4 pages

Practical No. 15

The document provides code for an Android application that displays a toast message when a button is clicked and includes checkboxes for selecting items with a button to order them. The MainActivity.java file contains the logic for displaying a toast message and calculating the total price based on selected checkboxes. The activity_main.xml file defines the layout with buttons and checkboxes for user interaction.

Uploaded by

ganeshkumbhar638
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)
5 views4 pages

Practical No. 15

The document provides code for an Android application that displays a toast message when a button is clicked and includes checkboxes for selecting items with a button to order them. The MainActivity.java file contains the logic for displaying a toast message and calculating the total price based on selected checkboxes. The activity_main.xml file defines the layout with buttons and checkboxes for user interaction.

Uploaded by

ganeshkumbhar638
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/ 4

Q. Write a program to display following toast MainAcitivity.

java
message.
activity_main.xml package com.example.practicalno15_1;
<?xml version="1.0" encoding="utf-8"?>
import android.os.Bundle;
<LinearLayout
import android.view.View;
xmlns:android="http://schemas.android.com/apk/r
import android.widget.Button;
es/android"
import android.widget.Toast;
xmlns:app="http://schemas.android.com/apk/res-
import
auto"
androidx.appcompat.app.AppCompatActivity;
xmlns:tools="http://schemas.android.com/tools"
public class MainActivity extends
android:layout_width="match_parent"
AppCompatActivity {
android:layout_height="match_parent"
Button b1;
tools:context=".MainActivity"
@Override
android:padding="20dp"
protected void onCreate(Bundle
android:gravity="center"
savedInstanceState) {
android:background="@color/white"
super.onCreate(savedInstanceState);
android:orientation="vertical">
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.btn);
<TextView
}
android:layout_width="wrap_content"
public void ShowToast(View v)
android:layout_height="wrap_content"
{
android:text="Hello World,Toast Message"
Toast.makeText(this, "Message for you \
android:textSize="20sp"
nYou have got Mail",
android:padding="20dp"
Toast.LENGTH_SHORT).show();
android:textColor="@color/black"
}
/>
}
<Button
android:id="@+id/btn"
android:text="Show Toast"
android:padding="20dp"
android:background="#ff00ff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:textSize="20dp"
android:textStyle="bold"
android:onClick="ShowToast"
/>
</LinearLayout>
xmlns:app="http://schemas.android.com/apk/re
s-auto"
xmlns:tools="http://schemas.android.com/tools
"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="20dp"
android:background="@color/white"
android:orientation="vertical">

<CheckBox
android:id="@+id/chk_pizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="40dp"
android:padding="10dp"
android:text="Pizza"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/chk_coffee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:padding="10dp"
android:layout_marginTop="20dp"
android:text="Coffee"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/chk_burger"
android:layout_width="wrap_content"
Q. Write a program to display three android:layout_height="wrap_content"
checkboxes and one button named "Order "as android:layout_marginLeft="40dp"
shown below. Once you click on button it android:padding="10dp"
should toast different selected checkboxes android:layout_marginTop="20dp"
along with items individual and total price. android:text="Burger"
android:textSize="20dp"/>

activity_main.xml <Button
<?xml version="1.0" encoding="utf-8"?> android:layout_width="wrap_content"
<LinearLayout android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/r android:id="@+id/btn"
es/android" android:text="ORDER"
android:onClick="onCheckboxclick"
android:background="#ff00ff" total+=150;
android:textColor="@color/white" }
android:textStyle="bold"
android:padding="25dp" if(chkcoffee.isChecked())
android:layout_gravity="center" {
android:layout_marginTop="30dp" result+=chkcoffee.getText().toString()+"
android:textAlignment="center" Rs.70/-\n";
android:textSize="25dp"/> total+=70;
</LinearLayout> }
if(chkburger.isChecked())
MainActivity.java {
result+=chkburger.getText().toString()+"
package com.example.practicalno15_2; Rs.100/-\n";
total+=100;
import android.os.Bundle; }
import android.view.View;
import android.widget.CheckBox; if(result!=null)
import android.widget.Toast; {
result=result.substring(0,result.length()-1);
import Toast.makeText(this, result,
androidx.appcompat.app.AppCompatActivity; Toast.LENGTH_LONG).show();
Toast.makeText(this,"Total
public class MainActivity extends Amt="+total,Toast.LENGTH_SHORT).show();
AppCompatActivity { }
CheckBox chkpizza, chkburger,chkcoffee; }
@Override }
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chkpizza = (CheckBox)
findViewById(R.id.chk_pizza);
chkburger = (CheckBox)
findViewById(R.id.chk_burger);
chkcoffee = (CheckBox)
findViewById(R.id.chk_coffee);
}
public void onCheckboxclick(View v) {

String result="";
int total=0;
if(chkpizza.isChecked())
{
result+=chkpizza.getText().toString()+"
Rs.150/-\n";

You might also like