Android CheckBox

In this tutorial, we will learn with the example how to use android checkbox. We will learn about different attributes that are used to customise this widget.

Android Checkbox can be described as below –

"CheckBox is a type of two state button either checked or unchecked. There can be a lot of use of checkboxes."

 CheckBox widget attributes are inherited from the TextView, View and Compound Button. Some of the popular attributes inherited from TextView are –





S. No. XML Attributes Description
1 android:backgroundTint Sets tint to the background.
2 android:clickable Sets true when you want to make View clickable. Otherwise, set false.
3 android:drawableBottom Drawable to be drawn at bottom of the text.
4 android:drawableEnd Drawable to be drawn to end of the text.
5 android:drawableLeft Drawable to be drawn to left of the text.
6 android:drawablePadding Padding of the drawable.



Attributes of CheckBox inherited from Compound Button are –

S. No. XML Attributes Description
1 android:button Drawable to be used for button graphic.
2 android:buttonTint Sets tint to button graphic.
3 android:buttonTintMode Blending mode used to apply the button graphic tint.

Attributes of CheckBox inherited from View are –





S. No. XML Attributes Description
1 android:id This is unique id of the Checkbox to uniquely identify the Checkbox.
2 android:padding Sets padding of this view.
3 android:onClick Defines the operations to perform when this view is clicked.
4 android:visibility Sets the visibility (visible, gone etc.) of the Checkbox.
5 android:tooltipText Defines text displayed in a small popup window on hover or long press.
6 android:background Sets background to this view.
7 android:alpha Sets alpha in view.

Follow steps below to create new android project. Please ignore the steps if you've already created android application.

S. No. Steps
1 Open Android Studio.
2 Go to File => New => New Project. Write application name as Checkbox. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button.
4 Then, select Empty Activity => click next => click finish.
5 If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit Tutorial to Create a New Project to know steps in detail.

Now, when you click CheckBox we will modify xml and java files to modify the action. Please follow the steps below.

Open res/values/strings.xml file. Then, add below code into it.

<string name="app_name">CheckBox</string>
<string name="check_it">Check it</string>

Other values folder have not been changed. So, we're not going to mention it here.





Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
xmlns:android="" xmlns:app=""
android:layout_height="match_parent" tools:context=".MainActivity"> 

app:layout_constraintTop_toTopOf="parent" />


Note that in xml file we have defined CheckBox widget with id CheckBox. Now, in java file we will access this checkbox widget. Then, when the CheckBox is clicked we will show a toast message.

Open src/main/java/com.ukacademe.checkbox/ file. Then, add below code into it.

package com.ukacademe.checkbox;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        CheckBox checkBox = findViewById(;
        if (checkBox != null) {
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String msg = "You have " + (isChecked ? "checked" : "unchecked") + " this Check This Checkbox.";
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();


Note that we have added a listener when the checked/unchecked state is changed in the checkbox. Since the file of AndroidManifest is very important for any android application. We are also going to mention it here.

Code inside main/AndroidManifest.xmlfile is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"
<activity android:name=".MainActivity">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>


When you run the application, you will get output as shown below.

UK Academe-CheckBox-Output