Android Tutorials


Friday, 11 October 2013

Android Checkbox Example

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(),


  1. I love this! Great job! I think kids explain things more clearly than adults sometimes!!!!! You should do Java tutorial.

  2. Yes unless you want a app to look like blank page you are going to need to learn XML.

  3. refer this link