android databinding: how to avoid onCheckedChanged triggered by programmatically

This is very simple with data binding

In xml checkbox component

 <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onCheckedChanged="@{(compoundButton, checked) -> 
changeKeyboardVM.onCheckedChange(compoundButton, checked)}" />

In ViewModel or Activity

  public void onCheckedChange(CompoundButton button, boolean check) {
    Log.d("Z1D1", "onCheckedChange: " + check);
}

now Boolean check true on checked and false on unchecked


I faced the same problem and i used onCLick listener instead onCHeck listener .That way the listener wont change the check state when it is set programatically. In your problem you should try setting different check change listeners to your check boxes.


Expose an ObservableBoolean from the ViewModel, then use two-way databinding over that boolean.

Then you can use the ObservableBoolean's values to decide what you want to do, rather than encode it in the XML.

android:checked="@={vm.checkedValue}"