0% found this document useful (0 votes)
15 views6 pages

Practical 15

The document contains two Android application implementations using XML and Java. The first part demonstrates a simple layout with a button that shows a toast message when clicked. The second part includes a layout with checkboxes for food items and a button that displays a summary of selected items and their total price in a toast message when clicked.

Uploaded by

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

Practical 15

The document contains two Android application implementations using XML and Java. The first part demonstrates a simple layout with a button that shows a toast message when clicked. The second part includes a layout with checkboxes for food items and a button that displays a summary of selected items and their total price in a toast message when clicked.

Uploaded by

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

Practical 15

Q1.
activity_main.xml
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Practical 15 - Toast"
android:textStyle="bold"
android:layout_marginBottom="20dp"
android:textSize="20sp"/>

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Content" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.prc15;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

Button showToastButton = findViewById(R.id.button);


showToastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Message for you: You have got mail!",
Toast.LENGTH_LONG).show();
}
});
}
}
Q2.
activity_main.xml
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:text="Pizza" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:text="Dal Bhatti" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:text="Civet Coffee" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Order"
android:layout_marginTop="20dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.prc15;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

CheckBox pizzaCheckBox = findViewById(R.id.checkBox);


CheckBox coffeeCheckBox = findViewById(R.id.checkBox2);
CheckBox burgerCheckBox = findViewById(R.id.checkBox3);
Button orderButton = findViewById(R.id.button);

orderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder orderSummary = new StringBuilder("Selected Items:\n");
int total = 0;

if (pizzaCheckBox.isChecked()) {
orderSummary.append("Pizza: ₹300\n");
total += 300;
}
if (coffeeCheckBox.isChecked()) {
orderSummary.append("Dal Bhatti: ₹200\n");
total += 200;
}
if (burgerCheckBox.isChecked()) {
orderSummary.append("Civer Coffee: ₹300\n");
total += 300;
}

orderSummary.append("Total: ").append(total).append("₹");
Toast.makeText(MainActivity.this, orderSummary.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}

You might also like