Android checkbox example

In Android, you can use “android.widget.CheckBox” class to render a checkbox.

In this tutorial, we show you how to create 3 checkboxes in XML file, and demonstrates the use of listener to check the checkbox state – checked or unchecked.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. Custom String

Open “res/values/strings.xml” file, add some user-defined string.

File : res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">Hello World, MyAndroidAppActivity!</string>
    <string name="app_name">MyAndroidApp</string>
    <string name="chk_ios">IPhone</string>
    <string name="chk_android">Android</string>
    <string name="chk_windows">Windows Mobile</string>
    <string name="btn_display">Display</string>

2. CheckBox

Open “res/layout/main.xml” file, add 3 “CheckBox” and a button, inside the LinearLayout.

File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:text="@string/chk_ios" />
        android:checked="true" />
        android:text="@string/chk_windows" />
        android:text="@string/btn_display" />
Make CheckBox is checked by default
Put android:checked="true" inside checkbox element to make it checked bu default. In this case, “Android” option is checked by default.

3. Code Code

Attach listeners inside your activity “onCreate()” method, to monitor following events :

  1. If checkbox id : “chkIos” is checked, display a floating box with message “Bro, try Android”.
  2. If button is is clicked, display a floating box and display the checkbox states.

File :

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MyAndroidAppActivity extends Activity {
  private CheckBox chkIos, chkAndroid, chkWindows;
  private Button btnDisplay;
  public void onCreate(Bundle savedInstanceState) {
  public void addListenerOnChkIos() {
	chkIos = (CheckBox) findViewById(;
	chkIos.setOnClickListener(new OnClickListener() {
	  public void onClick(View v) {
                //is chkIos checked?
		if (((CheckBox) v).isChecked()) {
		 	   "Bro, try Android :)", Toast.LENGTH_LONG).show();
  public void addListenerOnButton() {
	chkIos = (CheckBox) findViewById(;
	chkAndroid = (CheckBox) findViewById(;
	chkWindows = (CheckBox) findViewById(;
	btnDisplay = (Button) findViewById(;
	btnDisplay.setOnClickListener(new OnClickListener() {
          //Run when button is clicked
	  public void onClick(View v) {
		StringBuffer result = new StringBuffer();
		result.append("IPhone check : ").append(chkIos.isChecked());
		result.append("\nAndroid check : ").append(chkAndroid.isChecked());
		result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
		Toast.makeText(MyAndroidAppActivity.this, result.toString(),

4. Demo

Run the application.

1. Result :

android checkbox demo 1

2. If “IPhone” is checked :

android checkbox demo2

3. Checked “IPhone” and “Windows Mobile”, later, click on the “display” button :

android checkbox demo3

Download Source Code

Download it – (15 KB)


  1. Android CheckBox JavaDoc
  2. Android CheckBox example
Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: payment plan()

  • Pingback: water ionizer()

  • Pingback: house blue()

  • Pingback: zak?ad szklarski piotrk√≥w trybunalski()

  • Pingback: pay per day loan plans()

  • Pingback: how to run plumbing for a bathroom()

  • Pingback: icc plumbing code books()

  • Pingback: i & s locksmiths opening times()

  • Pingback: electrician practice test()

  • Pingback: water ionizer()

  • Pingback: water ionizer()

  • Pingback: car parking()

  • Pingback: car parking()

  • Pingback: mp4 mobile porn()

  • Pingback: Direct TV()

  • Pingback:

  • Pingback: kangen water machine()

  • Pingback: kangen water()

  • Pingback: water ionizer()

  • Pingback: water ionizers()

  • Pingback: watch tv show episodes()

  • Pingback: watch tv show episodes()

  • Pingback: streaming movies()

  • Pingback: watch tv show episodes()

  • Pingback: Details()

  • Pingback: Blue Coaster33()

  • full

    Thank you man, it helped me :)

  • full

    Thank you man it helped me

  • robert

    i have problem with this one

    chkIos = (CheckBox) findViewById(;

    and of course

    chkIos = (CheckBox) findViewById(;
    chkAndroid = (CheckBox) findViewById(;
    chkWindows = (CheckBox) findViewById(;
    btnDisplay = (Button) findViewById(;

    error about “chkIos cannot be resolved or is not a field”

  • Kaamz

    As a beginner I like this site. Easy to understand each topic in detail..

    Learned lot from here…. Hats off to U…

  • Abdul Latheef.A

    Dear friend
    Very Good tutorial and simple to understand.
    Thank u

  • Believer

    Hi, I’m a software developer just like you. I just want to congratulate you for this fantastic work of delivering concrete and useful source code. I’m know that there is more of you that can be appreciated in your web pages. I don’t know if you are Christian and/or believe in The Lord Jesus Christ, but I will certainly include you in my prayers, as a Christian Catholic. God be with you, Lord Jesus Christ always protect you, and give you the strength to contribute more to the SW Developer Community with this kind of work. Thank you, and God bless you.

    • Reece

      Just stop. You’re devaluing the word “developer” by preaching your beliefs. This page/post isn’t about religion so don’t bother mentioning it, you just look like a fool.

    • Minyong

      Oi kirig wag dito!

  • http://[email protected] devendra

    Very good site for freshers many thanks for MKYONG.

  • Rick

    How can I check dynamic checkbox ?

    CheckBox cb = new CheckBox(this);
    		cb.setPadding(12, 0, 15, 0);
  • Frank

    Thanks for your examples for android! ;)

    Frank (Germany)

  • Pingback: Android Followers » Android Tutorial From

  • onks_

    This was a tad helpful…thanks mkyong :-)