MAD CO6I Practical 11
Name: Pranjal Shahane Roll No: 06
Practical: 11
Q. Write a program to show five checkboxes and toast selected
checkboxes.
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maharastra"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haryana"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manipur"/>
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telangana"/>
Gramin Technical & managemnet campus 1
MAD CO6I Practical 11
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kerala"/>
<Button
android:id="@+id/buttonShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected Checkboxes"/>
</LinearLayout>
Java code:
package com.example.myapplication;
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 {
private CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5;
private Button buttonShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
checkBox3 = findViewById(R.id.checkBox3);
checkBox4 = findViewById(R.id.checkBox4);
checkBox5 = findViewById(R.id.checkBox5);
buttonShow = findViewById(R.id.buttonShow);
buttonShow.setOnClickListener(new View.OnClickListener() {
@Override
Gramin Technical & managemnet campus 2
MAD CO6I Practical 11
public void onClick(View v) {
StringBuilder selectedCheckboxes = new StringBuilder("Selected Checkboxes:\n");
if (checkBox1.isChecked()) {
selectedCheckboxes.append("Maharashtra\n");
}
if (checkBox2.isChecked()) {
selectedCheckboxes.append("Haryana\n");
}
if (checkBox3.isChecked()) {
selectedCheckboxes.append("Manipur\n");
}
if (checkBox4.isChecked()) {
selectedCheckboxes.append("Telangana\n");
}
if (checkBox5.isChecked()) {
selectedCheckboxes.append("Kerala\n");
}
// Show toast with selected checkboxes
Toast.makeText(MainActivity.this, selectedCheckboxes.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
Gramin Technical & managemnet campus 3
MAD CO6I Practical 11
OUTPUT:
Gramin Technical & managemnet campus 4