androiddata-bindingandroid-switch

How to use data binding for Switch onCheckedChageListener event?


As question indicates, how to bind checked change listener to Switch button in xml ?

I am not using recycler view. Just a simple layout.

Any help appreciated.


Solution

  • You can do it with a method reference:

    <CheckBox android:onCheckedChanged="@{callback::checkedChangedListener}".../>
    

    or with a lambda expression if you want to pass different parameters:

    <CheckBox android:onCheckedChanged="@{() -> callback.checked()}".../>