Android Checkbox Example

414

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

2. CheckBox

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

File : res/layout/main.xml

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 : MyAndroidAppActivity.java

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

NO COMMENTS

LEAVE A REPLY