Browse By

Android Check Box Example

Android Check Box is a button with two states—checked or unchecked. You often use CheckBox controls to turn a feature on or off or to pick multiple items from a list.

The check box button is often used in lists of items where the user can select multiple items. The Android check box contains a text attribute that appears to the side of the check box. This is used in a similar way to the label of a basic button. In fact, it’s  basically a TextView next to the button.

In this tutorial we will create simple android application which will display list of food items in checkbox, when user click on any checkbox, we will display placed order items and total bill in text view.

Direct Download Full Source Code



Android Check Box Example

Create New Android Project

  1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
  2. Open  main.xml (res/layout) and checkbox for each food item.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    	android:orientation="vertical" android:layout_width="fill_parent"
    	<TextView android:textColor="#fff" android:textSize="20dp"
    		android:layout_height="wrap_content" android:id="@+id/textView1"
    		android:layout_width="wrap_content" android:layout_gravity="center_horizontal"
    		android:text="Pulse 7 "></TextView>
    	<TextView android:textColor="#fff" android:textSize="15dp"
    		android:layout_height="wrap_content" android:id="@+id/textView2"
    		android:layout_width="wrap_content" android:layout_weight="0"
    		android:layout_gravity="center" android:text="Select Food Item "></TextView>
    	<CheckBox android:text="Pizza" android:layout_width="wrap_content"
    		android:layout_height="wrap_content" android:id="@+id/chkPizza"></CheckBox>
    	<CheckBox android:text="Burger" android:layout_width="wrap_content"
    		android:layout_height="wrap_content" android:id="@+id/chkBurger"></CheckBox>
    	<CheckBox android:text="Pasta" android:layout_width="wrap_content"
    		android:layout_height="wrap_content" android:id="@+id/chkPasta"></CheckBox>
    	<CheckBox android:text="Ice Cream" android:layout_width="wrap_content"
    		android:layout_height="wrap_content" android:id="@+id/chkIce"></CheckBox>
    	<TextView android:layout_width="wrap_content"
    		android:layout_height="wrap_content" android:id="@+id/lblOrder"
    		android:textColor="#fff" android:textSize="20dp"></TextView>
    	<TextView android:layout_width="wrap_content"
    		android:layout_height="wrap_content" android:id="@+id/lblTotal"
    		android:textSize="20dp" android:textColor="#fff"></TextView>
  3. Now open your Activity Class and attached listener setOnCheckedChangeListener for all checkbox to listen state of checkbox.
    package com.vrs.checkbox;
    import android.os.Bundle;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.TextView;
    public class Pulse7CheckBoxActivity extends Activity implements
    		OnCheckedChangeListener {
    	// Widget GUI
    	CheckBox chkPizza, chkPasta, chkBurger, chkIce;
    	TextView lblOrder, lblTotal;
    	// Activity Variable
    	int total = 0;
    	int pizzaCost = 50, pastaCost = 45, burgerCost = 40, iceCost = 55;
    	String order = "Placed Order :";
    	String totalBill = "Total Bill is : ";
    	/** Called when the activity is first created. */
    	public void onCreate(Bundle savedInstanceState) {
    		// Init Widget GUI
    		chkPizza = (CheckBox) findViewById(;
    		chkPasta = (CheckBox) findViewById(;
    		chkBurger = (CheckBox) findViewById(;
    		chkIce = (CheckBox) findViewById(;
    		lblOrder = (TextView) findViewById(;
    		lblTotal = (TextView) findViewById(;
    		// Attached Listener to checkbox
    	// / Listen Check box status change event and perform action accordingly
    	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    		if (buttonView == chkPizza) {
    			if (isChecked) {
    				total = total + pizzaCost;
    				order = order + " " + buttonView.getText();
    			} else {
    				total = total - pizzaCost;
    				order = order.replace(buttonView.getText(), "");
    		if (buttonView == chkPasta) {
    			if (isChecked) {
    				total = total + pastaCost;
    				order = order + " " + buttonView.getText();
    			} else {
    				total = total - pastaCost;
    				order = order.replace(buttonView.getText(), "");
    		if (buttonView == chkBurger) {
    			if (isChecked) {
    				total = total + burgerCost;
    				order = order + " " + buttonView.getText();
    			} else {
    				total = total - burgerCost;
    				order = order.replace(buttonView.getText(), "");
    		if (buttonView == chkIce) {
    			if (isChecked) {
    				total = total + iceCost;
    				order = order + " " + buttonView.getText();
    			} else {
    				total = total - iceCost;
    				order = order.replace(buttonView.getText(), "");
    		lblTotal.setText(totalBill + total);
  4. Now execute application
    Android Checkbox Example

    Select Check box to see placed order list and total bill

    Android Checkbox Example

    Bill and placed item is calculated when user check / uncheck on checkbox

Points to Remember

  • setOnCheckedChangeListener() method allows to you to attached check box change listener.
  • onCheckedChanged() method of OnCheckedChangeListener allows you to listen check box change event.
  • isChecked() methods returns true if checkbox is checked else false.
  • setChecked() method allows you to check / uncheck checkbox

I hope you like this article. Share your views to to improve content.

Download Full Source Code