3

I have the following RangeSlider and I am using DataBinding to provide the minimum / maximum value of the slider, as it may change while the user is on the screen.

RangeSlider

layout.xml

<layout ...>
    <data>

        <variable
            name="item"
            type="MyDataItem" />
    </data>

    ...

        <com.google.android.material.slider.RangeSlider
            android:id="@+id/my_slider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:stepSize="1"
            android:valueFrom="@{item.minimum}"
            android:valueTo="@{item.maximum}"
            ... />
</layout>

MyDataItem:

data class MyDataItem() {
    val minimum = MutableLiveData(Int.MIN_VALUE)
    val maximum = MutableLiveData(Int.MAX_VALUE)
}

However, whenever the app tries to inflate the view I get java.lang.IllegalStateException: valueFrom(0.0) must be smaller than valueTo(0.0)

hata
  • 11,633
  • 6
  • 46
  • 69
Sampson
  • 662
  • 6
  • 17

2 Answers2

2

Here is a complete solution how to implement two-way databinding with RangeSlider

ViewModel/Presenter:

var sliderRange = MutableLiveData<List<Float>>().apply { value = listOf(5f, 90f) }

Put there your initial data

Then, layout:

<com.google.android.material.slider.RangeSlider
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stepSize="1"
    android:valueFrom="1"
    android:valueTo="100"
    app:values="@={vm.sliderRange}" />

And finally how to put things together; BindingAdapter with InverseBindingAdapter :

@InverseBindingAdapter(attribute = "values")
fun getRangeSlider(slider: RangeSlider): List<Float> {
    return slider.values
}

@BindingAdapter("app:valuesAttrChanged")
fun setListeners(
    slider: RangeSlider,
    attrChange: InverseBindingListener
) {
    val listener = RangeSlider.OnChangeListener { _, _, _ -> attrChange.onChange() }
    slider.addOnChangeListener(listener)
}

Good luck,'.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
0

The Int.MIN_VALUE return a value of 2^-31 so that is smaller than 0.

And Int.MAX_VALUE return a value of 2^31 so that is bigger than 100.

RangeSlider limits are from 0 to 100. Try to use some values from 0 to 100.

Emanuel Garcia
  • 325
  • 2
  • 11
  • `RangeSlider`'s max is not limited to 100, as I've successfully set it to the numeric value of `Int.MAX_VALUE` directly in the xml. The issue appears to be with the LiveData, since it's trying to set `valueFrom` to 0 for some reason, despite my LiveData not even being initialized with 0. – Sampson Aug 09 '21 at 13:00