0% found this document useful (0 votes)
42 views4 pages

Practical 11

The document describes a program with XML code and Java code to create an Android application with five checkboxes and a button. When the button is clicked, a toast message will display the names of any checkboxes that are selected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Practical 11

The document describes a program with XML code and Java code to create an Android application with five checkboxes and a button. When the button is clicked, a toast message will display the names of any checkboxes that are selected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like