3

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
hans_dc
  • 529
  • 1
  • 5
  • 15
  • why do you need AppCompatCheckBox when CheckBox can do the same task – Umar Hussain Mar 07 '18 at 12:19
  • I don't really need it, but Android Studio gives me an error lint message saying 'this custom view should extend android.support.v7.widget.AppCompatCheckbox instead'. And because I don't immediately see any particular changes to the onCheckedChange code in this AppCompatCheckbox class, I'm wondering why this is not working any more in this class. – hans_dc Mar 07 '18 at 12:31
  • And I expect that my app behaviour stays the same if I change my class to inherit from AppCompatCheckbox instead of Checkbox (as android studio suggests). But that's not the case. That's why I'm asking the question, but apparently it is worth a downvote! – hans_dc Mar 07 '18 at 12:39

1 Answers1

2

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"/>
Roman
  • 470
  • 2
  • 5
  • 17