0% found this document useful (0 votes)
65 views3 pages

Practical No 11 (MAD)

The document outlines a practical exercise involving an Android application with five checkboxes and a button. When the button is clicked, it displays a toast message showing the selected checkboxes. The provided code includes both the XML layout and Java code necessary for implementation.

Uploaded by

vhsvhs152
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)
65 views3 pages

Practical No 11 (MAD)

The document outlines a practical exercise involving an Android application with five checkboxes and a button. When the button is clicked, it displays a toast message showing the selected checkboxes. The provided code includes both the XML layout and Java code necessary for implementation.

Uploaded by

vhsvhs152
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/ 3

Practical No 11

Q1. Write a program to show five checkboxes and toast selected checkboxes

Xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 4" />
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 5" />

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:layout_marginTop="16dp" />

</LinearLayout>
Java file
package com.example.madprac11;

import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


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

CheckBox checkBox1 = findViewById(R.id.checkBox1);


CheckBox checkBox2 = findViewById(R.id.checkBox2);
CheckBox checkBox3 = findViewById(R.id.checkBox3);
CheckBox checkBox4 = findViewById(R.id.checkBox4);
CheckBox checkBox5 = findViewById(R.id.checkBox5);
Button btnSubmit = findViewById(R.id.btnSubmit);

btnSubmit.setOnClickListener(v -> {
ArrayList<String> selectedOptions = new ArrayList<>();

if (checkBox1.isChecked())
selectedOptions.add(checkBox1.getText().toString());
if (checkBox2.isChecked())
selectedOptions.add(checkBox2.getText().toString());
if (checkBox3.isChecked())
selectedOptions.add(checkBox3.getText().toString());
if (checkBox4.isChecked())
selectedOptions.add(checkBox4.getText().toString());
if (checkBox5.isChecked())
selectedOptions.add(checkBox5.getText().toString());

String message = selectedOptions.isEmpty() ? "No option selected"


: "Selected: " + String.join(", ", selectedOptions);

Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();


});
}
}
Output:

You might also like