Is it possible to use data-binding for a SeekBar or ProgressBar in Android? I have this data element:
To get the TextView to update when the SeekBar changes, bind to progress change through the android:onProgressChanged
attribute. This expects a public method in the Model with the signature of SeekBarBindingAdapter.OnProgressChanged
:
View
Model
public class Model {
public ObservableField seekBarValue = new ObservableField<>("");
public void onValueChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
seekBarValue.set(progresValue + "");
}
}
In general I advice you to look at the source code for all the Adapter classes made for Data Binding. If you for instance navigate to the file SeekBarBindingAdapter.java
in Android Studio (menu Navigate>File) you'll learn all the event methods you can bind to through the adapters there. This particular feature is available because Google have implemented the following adapter:
@BindingAdapter("android:onProgressChanged")
public static void setListener(SeekBar view, OnProgressChanged listener) {
setListener(view, null, null, listener);
}