Search code examples
androidandroid-appcompatandroid-checkboxoncheckedchanged

onCheckedChanged not called on AppCompatCheckBox view


I create some custom checkbox views dynamically in code. Those checkboxes inherit from AppCompatCheckBox.

public class ChangeableSelectionParameterValueCheckbox extends android.support.v7.widget.AppCompatCheckBox

I add the OnCheckChangedListener in code. The added OnCheckedChangeListener is a android.widget.CompoundButton.OnCheckedChangeListener;

changeableSelectionParameterValueCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //on checked changed code here
            }
        });

The problem: onCheckedChanged is never called. The custom checkbox seems to be not clickable.

When I change my custom view class to inherit from android.widget.CheckBox, everything works fine.

Any suggestions?


Solution

  • I had the same problem and solved it by setting the property "clickable" to true. It worked both in code and in XML, so choose one of these solutions:

    AppCompatCheckbox check;
    check.setClickable(true);
    
    <android.support.v7.widget.AppCompatCheckBox xmlns:android="http://schemas.android.com/apk/res/android"
                                                 android:id="@+id/id_checkbox"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="wrap_content"
                                                 android:clickable="true"
                                                 android:gravity="center_vertical"/>