---
title: "Checkbox in Android"  
description: "By reading this article you will get to know about Checkbox Control which is used for taking data from the user in multiple ways."  
author: "Nitish Kesarwani"  
published: 2017-12-23  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12740/checkbox-in-android  
category: "android apps"  
tags: ["android", "android activity", "android checkbox", "onclicklistener", "android controls"]  
reading_time: 5 minutes  

---

# Checkbox in Android

**By reading this article you will get to know about [Checkbox Control](https://www.mindstick.com/articles/291/how-should-use-checkbox-control-in-vb-dot-net) which is used for taking data from the user in [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) ways.**

1. It has two states either checked or unchecked.

2. It has so many [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) such as computers or Phones, tablet.

3. I am using a single button and three checkboxes which are declared in Activity_Main.xml.

4. As we know the [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net) are [controlled](https://yourviews.mindstick.com/view/81837/has-pakistan-controlled-corona-pandemic) by the java code which is used in MainActivity.java.

5. In java file, the method buttonChecked() is used to invoke when [Activity](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android) is created.

6. Methods introduced are onClick, setOnclickListener and followed by view.onClickListener.

7. [StringBuilder](https://www.mindstick.com/blog/99/stringbuilder-class-in-c-sharp) class is used to build the modified String with append method. It is used to get the message on the screen with the help of TextView and Toast.

8. isChecked method is used to verify, that either it has used click on following checkboxes or not.

9. Declare menu is inside the [directory](https://www.mindstick.com/forum/226/how-to-get-application-directory-using-c-sharp-csharp) res\menu. If it is not given there then you can create it manually.

##### Attributes

Each [attribute](https://www.mindstick.com/blog/61/ado-dot-net-object-model-and-attributes) is inherited from android.Widget.TextView

o **android: autoText:** when this attribute is set then it specifies TextView in a Textual input method and check the common spelling error.

o **android:drawableBottom:** This is drawable [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) which are specified at the bottom.

o **android:drawableRight:** This is drawable elements which are specified in the right

o **android:editable:** Attribute provides the editable TextView input.

###### AndroidManifest.xml

```
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.checkbox">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest> 
```

Main_Activity.xml

```
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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"
    tools:context="com.mapsample.msclient009.checkbox.MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tv"/>
    <CheckBox
        android:id="@+id/color1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Red"
        />
    <CheckBox
        android:id="@+id/color2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Green" />
    <CheckBox
        android:id="@+id/color3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Blue"
        />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="itemSelected"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="34dp"
        />

</LinearLayout>
```

Main_Activity.java

```
package com.mapsample.msclient009.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
CheckBox r,g,b;
Button btn;
TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
       buttonChecked ();
    }
public void buttonChecked()
{ r =(CheckBox)findViewById ( R.id.color1 );
    g =(CheckBox)findViewById ( R.id.color2 );
    b =(CheckBox)findViewById ( R.id.color3 );
    txt=(TextView)findViewById (R.id.tv );
    btn =(Button)findViewById (R.id.btn );
   btn.setOnClickListener ( new View.OnClickListener ( ) {
       @Override
       public void onClick(View v) {
           int count = 0;
           StringBuilder builder = new StringBuilder ( );
          builder.append ( "<<<---Select Item--->>>" );
           if(r.isChecked ()) {
               builder.append ( "Red" );
               count++;
           }
           if(g.isChecked ())
           {builder.append ( "Green" );
             count++;
           }
           if(b.isChecked ())
           { builder.append ("Blue" );
               count++;

           }

           Toast.makeText ( getApplicationContext (),count+"Item name is \n"+builder.toString (),Toast.LENGTH_LONG ).show ();
       txt.setText ( count+"colors\n"+builder.toString ()+"\nis Selected" );
       }
   } );
}

public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater ().inflate (R.menu.menu_activity,menu );
    return true;
}

} 
```

\

```
Menu.xml<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="red">red</item>
  <item android:title="green">Green</item>
   <item android:title="blue">Blue</item>
</menu>
```

\

##### Launch Your APP

![Checkbox in Android](https://www.mindstick.com/mindstickarticle/d228c892-4abc-4d8f-a3b9-16781577ac17/images/5eb44211-0a99-4749-b419-35566ff635fe.png)![Checkbox in Android](https://www.mindstick.com/mindstickarticle/d228c892-4abc-4d8f-a3b9-16781577ac17/images/ca7b9ee3-72d7-4bc1-bf12-e28ea883f000.png)\
\

---

Original Source: https://www.mindstick.com/articles/12740/checkbox-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
