In this article I am going to explain how to create a checkbox control in android application by using CheckBox widget. When the CheckBox is pressed, toast message will indicate the current state of the checkbox and the text of the checkbox will change.

·         Start a new project, named CheckBoxDemo.

·         Open res/layout/main.xml and add the checkbox as shown below:

<?xml  version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    <CheckBox android:id="@+id/checkbox"
                android:text="Check me"/>

·         Open the activity file and add the following code as given below:

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
public class CheckBoxActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        final CheckBox checkbox=(CheckBox)findViewById(;
        checkbox.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                           Toast.makeText(CheckBoxActivity.this, "Selected", 
                           checkbox.setText("Unchecked me");
                           Toast.makeText(CheckBoxActivity.this, "Not Selected",
                           checkbox.setText("Checked me");

This captures the CheckBox element from the layout, and then adds a View.OnClickListener. The View.OnClickListener must implement the onClick(View) callback method, which defines the action to be made when the checkbox is clicked. When clicked, isChecked() is called to check the new state of the check box. If it has been checked, then a Toast displays the message "Selected" and change the checkbox text to "Unchecked me”, otherwise it displays "Not selected" and change the checkbox text to "Checked me". 

·         Run an application
Your output is something like this:

Checkbox control in Android Application

When you checked the checkbox, a toast display a message "Selected" and checkbox text will change into "Unchecked me".

Checkbox control in Android Application

Again, when you unchecked the checkbox, a toast display a message "Not Selected" and checkbox text will change into "Checked me".

  Modified On Nov-30-2017 05:53:23 AM

Leave Comment