Search code examples
androidandroid-databinding2-way-object-databinding

How to get notified when property changes in Android databinding?


I would like to get notified when property changes in my variable.

In my Activity:

    viewDataBinding = DataBindingUtil.setContentView(this, R.layout.list);
    viewDataBinding.setViewFilter(viewFilter);

My model:

public class ViewFilter extends BaseObservable {
    private String priceFrom;

    public ViewFilter() {
    }

    @Bindable
    public String getPriceFrom() {
        return priceFrom;
    }

    public void setPriceFrom(String priceFrom) {
        this.priceFrom = priceFrom;
        notifyPropertyChanged(BR.priceFrom);
    }
}

My layout:

<data>
    <variable
        name="viewFilter"
        type="com.example.ViewFilter" />
</data>

                <EditText
                    android:id="@+id/edtListPriceFrom"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:digits="0123456789."
                    android:inputType="numberDecimal|numberSigned"
                    android:text="@={viewFilter.priceFrom}" />

When I want to add listener:

    viewFilter.addOnPropertyChangedCallback(new OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {

It doesn't get called.

What should I do to have working property change notifications?


Solution

  • As suggested in comments - I was registering callback on other object instance and that's why I did not receive any.