Is it possible to use data-binding for a SeekBar or ProgressBar in Android? I have this data element:
<data>
<variable
name="foo"
type="com.example.Foo" />
</data>
If I refer to the variable foo.bar (which is an int) in a TextField, it works as expected:
<TextView
android:text="@{String.valueOf(foo.bar)}"
[...]
What I tried to do was this:
<SeekBar
android:progress="@{foo.bar}"
[...]
but it wasn't recognized. (As soon as I wrote an "@" between the quotes, it got red in the editor). Is there another way of data-binding it?
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
<TextView
android:text="{@model.seekBarValue} />
<SeekBar
android:onProgressChanged="@{model.onValueChanged}" />
Model
public class Model {
public ObservableField<String> 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);
}