'Cannot remove padding from Android Material slider

I've created a slider and I want to remove the padding. It seem it's not possible. I know there is some padding necessary for the thump, but I'd like to take care of the padding myself.

        <com.google.android.material.slider.Slider
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="0dp" />

Even with setting padding to 0 it still shows some padding. It seems the padding parameter is ignored. It also seems the thumb diameter is not taken into account for the padding.

image taken from layout inspector

How can I set the padding? What if I want a real big thumb diameter?



Solution 1:[1]

If you add <dimen name="mtrl_slider_track_side_padding">0dp</dimen> in your dimens.xml, the paddings is gone, Is not the ideal solution but maybe works for you.

Solution 2:[2]

all dimensions is here

you can override all of them in your project dimens.xml

Solution 3:[3]

This property is named "trackSidePadding" in BaseSlider which is the parent of the Slider class

You can use java reflection to change it:

kotlin:

val field: Field = slider.javaClass.superclass.getDeclaredField("trackSidePadding")

field.isAccessible = true

field.set(slider, 'YOUR INT VALUE')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AndroidRuntimeException
Solution 2 Ali Zarei
Solution 3 M_rey3000