Cannot remove padding from Android Material slider

Viewed 737

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?

3 Answers

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.

all dimensions is here

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

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')
Related