Professional Documents
Culture Documents
PR No 15-Mad
PR No 15-Mad
XML CODE :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/button_show_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_centerInParent="true"/>
</RelativeLayout>
JAVA CODE :
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
PR NO 15 : Page 1 of 6
public class MainActivity extends AppCompatActivity {
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonShowToast.setOnClickListener(new View.OnClickListener() {
@Override
});
PR NO 15 : Page 2 of 6
Q no 2 : 2. Write a program to display three checkboxes and one button named "Order "as shown
below. Once you click on button it should toast different selected checkboxes along with items
individual and total price
Xml code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<CheckBox
android:id="@+id/checkbox_pizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza"
PR NO 15 : Page 3 of 6
android:checked="false" />
<CheckBox
android:id="@+id/checkbox_coffee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox_pizza"
android:text="Coffee"
android:checked="false" />
<CheckBox
android:id="@+id/checkbox_burger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox_coffee"
android:text="Burger"
android:checked="false" />
<Button
android:id="@+id/button_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox_burger"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Order" />
</RelativeLayout>
Java code :
import android.os.Bundle;
import android.view.View;
PR NO 15 : Page 4 of 6
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkboxPizza = findViewById(R.id.checkbox_pizza);
checkboxCoffee = findViewById(R.id.checkbox_coffee);
checkboxBurger = findViewById(R.id.checkbox_burger);
buttonOrder = findViewById(R.id.button_order);
buttonOrder.setOnClickListener(new View.OnClickListener() {
@Override
orderItems();
});
PR NO 15 : Page 5 of 6
// Method to process ordered items and display toast message
double totalPrice = 0;
if (checkboxPizza.isChecked()) {
totalPrice += 10;
if (checkboxCoffee.isChecked()) {
totalPrice += 5;
if (checkboxBurger.isChecked()) {
totalPrice += 8;
PR NO 15 : Page 6 of 6