---
title: "Checkbox control in Android Application"  
description: "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,"  
author: "Chris Anderson"  
published: 2011-10-22  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/789/checkbox-control-in-android-application  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Checkbox control in Android Application

In this article I am going to [explain](https://www.mindstick.com/forum/159699/what-is-a-compilation-error-in-dot-net-core-explain-with-an-example) 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="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <CheckBox android:id="@+id/checkbox"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Check me"/>
</LinearLayout>
```

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

```
import android.app.Activity;
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 {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         final CheckBox checkbox=(CheckBox)findViewById(R.id.checkbox);
         checkbox.setOnClickListener(new OnClickListener() {
               public void onClick(View v) {
                      if(((CheckBox)v).isChecked())
                     {
                           Toast.makeText(CheckBoxActivity.this,  "Selected",
                                                          Toast.LENGTH_SHORT).show();
                           checkbox.setText("Unchecked me");
                     }
                      else
                     {
                           Toast.makeText(CheckBoxActivity.this,  "Not Selected",
                                                         Toast.LENGTH_SHORT).show();
                           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](https://www.mindstick.com/mindstickarticle/70ccec4f-65e4-405d-88de-98fd6395823c/images/95515157-dd89-40a5-9095-d892feec7115.png)

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

![Checkbox control in Android Application](https://www.mindstick.com/mindstickarticle/70ccec4f-65e4-405d-88de-98fd6395823c/images/e2cdc868-3d1c-4908-a9c1-a60e98e97a4d.png)

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

\

---

Original Source: https://www.mindstick.com/articles/789/checkbox-control-in-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
