How to use CheckBox in Android
Last Updated :
29 Jan, 2025
CheckBox belongs to android.widget.CheckBox class. Android CheckBox class is the subclass of CompoundButton class. It is generally used in a place where user can select one or more than choices from a given list of choices.
Syntax of CheckBox
public class CheckBox extends CompoundButton
It has two states - checked or unchecked.
Class Hierarchy :
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.Button
↳ android.widget.CompoundButton
↳ android.widget.CheckBox
Methods of CheckBox class
- public boolean isChecked(): If CheckBox is in checked state then return true otherwise false.
- public void setChecked(boolean status): It changes the state of the CheckBox.
Example of CheckBox in Android
Below is the code for an example where the user chooses its hobbies from the given list containing Painting, Reading, Singing and Cooking with the help of CheckBox.
1. Working of Layout
The activity_main.xml has a TextView, 4 CheckBoxes and a button.The TextView prompts the user to select his/her hobbies. First user select its choices and then presses the Submit button. After pressing Submit button, a toast will generate showing the selected hobbies.
activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="Choose your hobbies:"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Painting"
android:layout_marginTop="16dp"
android:textSize="16sp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reading"
android:layout_marginTop="16dp"
android:textSize="16sp" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Singing"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="@+id/textView"
tools:layout_editor_absoluteX="382dp" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cooking"
android:layout_marginTop="16dp"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@+id/checkBox"
tools:layout_editor_absoluteX="386dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="Check"
android:text="submit" />
</LinearLayout>
Layout:
Note: The Application is using android:onClick="Check" in xml file which help us to call the Check method when the button is clicked.
2. Modification of the MainAcitivity File
Now, after creating the layout we want to add the functionality to the application/layout.
MainActivity File:
Java
package com.gfg.checkbox;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
CheckBox ch,ch1,ch2,ch3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch=findViewById(R.id.checkBox);
ch1=findViewById(R.id.checkBox2);
ch2=findViewById(R.id.checkBox3);
ch3=findViewById(R.id.checkBox4);
}
public void Check(View v){
String msg="Selected Items are";
// Checking the selection
if(ch.isChecked())
msg=msg+" [Painting]";
if(ch1.isChecked())
msg=msg+", [Reading]";
if(ch2.isChecked())
msg=msg+", [Singing]";
if(ch3.isChecked())
msg=msg+", [Cooking]";
// Executing the Toast
Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
// Clearing all the selection
ch.setChecked(false);
ch1.setChecked(false);
ch2.setChecked(false);
ch3.setChecked(false);
}
}
Kotlin
package com.gfg.checkbox_kotlin
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
class MainActivity : AppCompatActivity() {
lateinit var ch: CheckBox
lateinit var ch1: CheckBox
lateinit var ch2: CheckBox
lateinit var ch3: CheckBox
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ch = findViewById(R.id.checkBox)
ch1 = findViewById(R.id.checkBox2)
ch2 = findViewById(R.id.checkBox3)
ch3 = findViewById(R.id.checkBox4)
}
fun Check(v: View?) {
var msg = "Selected Items are"
// Checking the selection
if (ch.isChecked) msg = "$msg [Painting]"
if (ch1.isChecked) msg = "$msg, [Reading]"
if (ch2.isChecked) msg = "$msg, [Singing]"
if (ch3.isChecked) msg = "$msg, [Cooking]"
// Executing the Toast
Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
// Clearing all the selection
ch.isChecked = false
ch1.isChecked = false
ch2.isChecked = false
ch3.isChecked = false
}
}
Output: ( Selecting and Pop-Up/Toast the Selection)