12

Is there any way to change slider thumb size? I think for now we can only manipulate colors

var sliderPosition by remember { mutableStateOf(0f) }
Text(text = sliderPosition.toString())
Slider(
    value = sliderPosition,
    onValueChange = { sliderPosition = it },
    valueRange = 0f..100f,
    onValueChangeFinished = {
        // launch some business logic update with the state you hold
        // viewModel.updateSelectedSliderValue(sliderPosition)
    },
    steps = 5,
    colors = SliderDefaults.colors(
        thumbColor = MaterialTheme.colors.secondary,
        activeTrackColor = MaterialTheme.colors.secondary
    )
)

Jetpack-Compose Slider

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
ysfcyln
  • 2,857
  • 6
  • 34
  • 61

4 Answers4

6

No, this size cannot be modified. The only thing you can do is copy the entire Slider.kt file into your project and modify it.

It is a good idea to give the new view a different name to avoid misunderstandings in the future.

You should change ThumbRadiusconstant, or make it a variable if you need different sizes in your application.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
6

With M3 androidx.compose.material3.Slider you can use the thumb attribute to customize the size.

Something like:

var sliderPosition by remember { mutableStateOf(0f) }
Column {
    Text(text = sliderPosition.toString())
    Slider(
        modifier = Modifier.semantics { contentDescription = "Localized Description" },
        value = sliderPosition,
        onValueChange = { sliderPosition = it },
        valueRange = 0f..5f,
        steps = 4,
        interactionSource = interactionSource,
        onValueChangeFinished = {
            // launch some business logic update with the state you hold
        },
        thumb = {
            SliderDefaults.Thumb( //androidx.compose.material3.SliderDefaults
                interactionSource = interactionSource,
                thumbSize = DpSize(40.dp,40.dp)
            )
        },

    )
}

enter image description here

Note: it requires for material3 at least the version 1.0.0-beta03

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

I've created a library for easy customization of Slider, since Slider from Material package is not flexible. https://github.com/krottv/compose-sliders. Below is the code example of how to use it to make thumb size smaller:

var stateSlider by remember { mutableStateOf(0.5f) }
SliderValueHorizontal(
    stateSlider, { stateSlider = it },
    modifier = Modifier
        .fillMaxWidth(),
    // desired size of Slider's thumb
    thumbSize = DpSize(8.dp, 8.dp)
)

Also you can specify custom composables for thumb and track.

  • 2
    Could you please add the most relevant parts of the code in your answer itself? Explanations would be great without having to visite an external site. Thanks. – Eric Aya Nov 19 '21 at 15:22
0

Yes, but only wrapping it with AndroidView and wait for the better future, when Google team release another update in Material lib.

Here is an example

  AndroidView(
    modifier = Modifier...//,
    factory = { context ->
      Slider(
        ContextThemeWrapper(context, context.theme)
      ).apply {
      // set listeners
      it.addOnSliderTouchListener(object : SliderView.OnSliderTouchListener {
        @SuppressLint("RestrictedApi")
        override fun onStartTrackingTouch(slider: Slider) = Unit

        @SuppressLint("RestrictedApi")
        override fun onStopTrackingTouch(slider: Slider) {
          onValueChangeFinished.invoke()
        }
      })
      it.addOnChangeListener { _, value, _ ->
        onValueChanged.invoke(value)
      }
        // your thumb customization
        // your track customization
      }
  }, update = {
      // set value
      it.value = currentValue
  })

Should be placed inside @Composable

Akbolat SSS
  • 1,761
  • 15
  • 23