Practical 11
Practical 11
<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"/>
<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;
@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
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");
}
OUTPUT: