mgmblog.com

February 18, 2008

Android CheckBox OnCheckedChangeListener

Filed under: Android — michael @ 1:42 pm

For whatever reason I was having problems getting this code to work, but finally played with it enough and thought I’d write a quick post so I had a reference for later. I was looking for code which demonstrated a listener for the Android CheckBox view widget. I saw some examples of the OnClickListener but was trying to implement the OnCheckedChangeListener. Here is the code I used

CheckBox repeatChkBx =
    ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
        }

    }
});

Part of the problem I was having was that the Android Eclipse plugin wasn’t recognizing the onCheckChanged method so the auto implementation wasn’t working, but when I pasted it in it worked fine.

7 Comments »

  1. Hi, im a new one for this android environment. Can any one tell me how to implement CheckBox? Is there any book or tutorial available for android?

    Comment by kumar — December 2, 2008 @ 10:01 am

  2. Hello Kumar,

    I’m still working on http://www.pragprog.com/titles/eband/hello-android, it hasn’t been published yet, but you can purchase a priliminary PDF right now. It seems pretty good so far with lots of good material for those just starting out. I’m looking forward to the OpenGL chapter and ContentProviders for data access on the Android platform.

    As far as using checkboxes, you have to either build your screens by hand and include a CheckBox object, or the preferred method is to create a layout XML file in the res directory. This layout file tells Android which widgets you want on a display and their layout. This is discussed in the Hello Android book and in Google’s Development documentation at, http://code.google.com/android/documentation.html . The CheckBox is specifically metioned in it’s API section at, http://code.google.com/android/reference/android/widget/CheckBox.html .

    Good luck, hope this helps!
    - michael

    Comment by michael — December 2, 2008 @ 10:51 am

  3. you can do as follows:

    cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
    // TODO Auto-generated method stub

    }
    });

    Comment by robert — August 18, 2009 @ 9:05 pm

  4. Great post! Helped a lot!

    Thanks :)
    Sara

    Comment by Sara — August 27, 2010 @ 4:36 am

  5. Just what I was looking for. Thanks.

    Comment by bhac — January 24, 2011 @ 5:06 am

  6. exactly what i wanted!

    Comment by anjali — May 17, 2011 @ 11:07 pm

  7. cool….
    Thanks….

    Comment by anup — June 6, 2011 @ 12:21 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress