How to receive a event on android checkbox check change?

Annerajb picture Annerajb · Jun 30, 2010 · Viewed 85.4k times · Source

What would be the correct way of receiving and sending an event when a check box gets enabled or disabled?

In C# I could just easily double click and all the code would be done for me. But in android it appears to be a bit more obscure. I thought of using the touch event handlers but then if the user has a keyboard it won't detect the change since it's not touch. I figure android should have a native event for check box state change.

Answer

Cristian picture Cristian · Jun 30, 2010
CheckBox repeatChkBx = ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
        }

    }
});