Android : Using CheckBox with Example

Introduction:

In this post we will learn to use checkboxes in Android Application.

What is a CheckBox ?

Checkboxes are the GUI elements which provide facility to select multiple options (from zero to all) from an available list of options. It is very useful while developing any kind of application and Android is not an exception. A CheckBox can have two states i.e, either checked or Unchecked . We can make our logic according to these two states of a checkbox. In general, it can be said that it is not much different from any HTML input(<input type=”checkbox” name=”name1″ value=”value1″ />), ASP.NET checkbox(<asp:CheckBox ID=”CheckBox1″ runat=”server”>) etc. with very few differences.

Pre-requisites :

The reader of this post doesn’t need to have a depth knowledge on Android apps development, in fact this is for beginners who have just started developing android apps and want to know the basics. This post may not help you in real-world project development but, is a short demonstration of CheckBox control in Android.

If you are a beginner and want to know the whole thing right from the start like installing Android SDK and creating your first application etc then you may check these post..

Installing Android SDK on windows – Complete steps

Creating ‘HelloWorld’ application in Android with Eclipse Indigo

Installing [dot]apk file on Emulator in Windows

Simple example of Intent

Using Button & Click-Event with example

CheckBox in Android :

To demonstrate a very simple example using checkbox,  I have dragged a TextView and two CheckBox to my main.xml and did the basic setting-up kind of things like id,text,textColor,background,width,height etc as per my need.

Now create a OnClickListener which will later listen to our on-click events on the two checkboxes. Then put a very simple logic in the onClick method to show text of the checked checkbox(s) in the TextView and let reset the text of the TextView if none of these two checkboxes are checked.

Code :

main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

<TextView
 android:id="@+id/tvDetails"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.17"
 android:textSize="22dp"
 android:background="@android:color/white"
 android:textColor="@android:color/black" />

<CheckBox
 android:id="@+id/cbSuvendu"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="@string/suvendu" />

<CheckBox
 android:id="@+id/cbWordPress"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="@string/wordpress" />

</LinearLayout>

strings.xml


<?xml version="1.0" encoding="utf-8"?>
 <resources>

<string name="hello">Hello World, CheckBoxTutorialActivity!</string>
 <string name="app_name">First CheckBox Tutorial</string>
 <string name="suvendu">www.suvendugiri.com</string>
 <string name="wordpress">www.wordpress.com</string>

</resources>

Activity.java


package com.suvendu.tutorial.cb;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

public class CheckBoxTutorialActivity extends Activity {
    TextView tv;
    CheckBox cbS;
    CheckBox cbW;
    OnClickListener checkBoxListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cbS=(CheckBox)findViewById(R.id.cbSuvendu);
        cbW=(CheckBox)findViewById(R.id.cbWordPress);
        checkBoxListener =new OnClickListener() {

            @Override
            public void onClick(View v) {
                tv=(TextView)findViewById(R.id.tvDetails);
                tv.setText("I Like ");
     
                if(cbS.isChecked()) {
                    tv.setText(tv.getText().toString()+" "+ cbS.getText().toString());
                }

                if(cbW.isChecked()) {
                    tv.setText(tv.getText().toString()+ " "+cbW.getText().toString());
                }

                if(!cbS.isChecked()&amp;&amp;!cbW.isChecked()) {
                    tv.setText("");
                }
            }
        };

        cbS.setOnClickListener(checkBoxListener);
        cbW.setOnClickListener(checkBoxListener);
    }
}

Download :

You can download the full project here. Download

Or, you can just download the apk here.Download

Conclusion :

Final words- Just download the project and run it using your emulator and if you have any doubts or suggestions, please place them in the comments. In the next post we will see the same example using radio buttons.

About these ads

33 thoughts on “Android : Using CheckBox with Example

  1. hi i try ur example above on how to use checkBox..
    And i have an error on CheckBoxListener… the error says CheckBoxListener cannot be resolved to a variable..
    can u help me?

  2. Hi Dilah,
    It should work. Please make sure that you have imported all namespaces.
    Otherwise just download the source and give it a try. If you still have an issue after this then please mail your project to suvendu.giri@live.com and let me check that.

    Thanks

  3. hello sir, can u please give me the code if i want to retrieve the value of multiple checkboxes (only those checkboxes which are checked) from one xml to another xml in textview form.
    any help would be appreciated. thanks in advance.

  4. hello im new at this and i have a problem in my listview ; i hve textviews in my listview and checkboxes and i want to use the value of the textview when the checkbox is checked can i post the code so anyone can help me plzz

  5. Hi. I have a problem with my checkBoxes. I wabted to make an option menu for settings that includes all the fields from a database that a customer would like to be displayed, using checkboxes. I have 2 problems:
    a) all the time when I try to acces the checkboxes it returns nullpointerException. (I am accesing them from another activity, different from the one that i use to display them)
    b) when i restart the application all the savings for the chexboxes are gone.

    I would appreciate you helps.
    Thanks,
    Stefan

  6. Nice post Suvendu, much needed
    but i want whenever user will click on checkbox then value of first textview send to another textview using image button onclick, please make this program….Thanks

  7. Hi Suvendu!! Thank you for your really good and clear explaination for CheckBox. I’m studying android programming but there were not suitable many examples to study for me here South Korea. If you don’t mind me asking, can I re-post android progrmming with revised your tutorial after adding my source? I promise that I disclose the sources your blog only for study.

    • Thanks Anamika for spending some time on my blog. I’ll definitely post more on android apps in future days and hopefully you’ll come back to read them.

  8. your post is very nice and helpfull.I have one problem,i want to change themes of buttons in application using checkbox.when ny checkbox is selected the theme of buttons will change.can you help me to get solution.thanx..

  9. Hello sir,your is very helpfull to me.I have one problem,i want to change themes of buttons in my application using checkbox.when i select any one of the theme then that theme should be apply to all buttons in my application.can you suggest solution for this.
    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s