androidx.compose.material3

Slider

Composable function

A slider picks a value from a range by dragging. Use it for settings like volume or brightness, where the exact number matters less.

The slider does not remember its value. You keep it in a variable and update it in onValueChange.

A slider from 0 to 1
A slider from 0 to 1
var volume by remember { mutableFloatStateOf(0.4f) }
Slider(value = volume, onValueChange = { volume = it }, modifier = Modifier.width(280.dp))

RangeSlider has two thumbs, for a start and an end. VerticalSlider goes up and down. See Slider types.

Pick a range with two thumbs
Pick a range with two thumbs
var price by remember { mutableStateOf(20f..60f) }
Column(Modifier.width(280.dp)) {
    Text("€${price.start.roundToInt()} – €${price.endInclusive.roundToInt()}")
    RangeSlider(value = price, onValueChange = { price = it }, valueRange = 0f..100f)
}

Basic

Takes value and onValueChange.

@Composable
fun Slider(
    value: Float,
    onValueChange: (Float) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
    steps: Int = 0,
    onValueChangeFinished: (() -> Unit)? = null,
    colors: SliderColors = SliderDefaults.colors(),
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
)

value: FloatRequired

The current value. Pass your state here. For a RangeSlider, a range like 20f..60f.

onValueChange: (Float) -> UnitRequired

Called with the new value while the user drags. Update your state here.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. Set the width (or height for a vertical slider) here.

enabled: BooleanOptional

Default: true

Set to false to make the slider gray and not draggable.

valueRange: ClosedFloatingPointRange<Float>Optional

Default: 0f..1f

The lowest and highest value. Default: 0f..1f.

steps: IntOptional

Default: 0

The number of stops between the ends. The thumb snaps to them. Default: 0, which means no stops. For 1 to 5 in whole numbers, use steps = 3.

Steps and a range
Steps and a range
var rating by remember { mutableFloatStateOf(3f) }
Column(Modifier.width(280.dp)) {
    Text("Rating: ${rating.roundToInt()}")
    Slider(
        value = rating,
        onValueChange = { rating = it },
        valueRange = 1f..5f,
        steps = 3, // 3 stops between 1 and 5: 2, 3 and 4
    )
}

onValueChangeFinished: (() -> Unit)?Optional

Default: null

Called once when the user lets go. Use it to save the value or start an action.

colors: SliderColorsOptional

Default: SliderDefaults.colors()

Sets thumb, track and tick colors. Make one with SliderDefaults.colors.

Tertiary colors
Tertiary colors
var value by remember { mutableFloatStateOf(0.7f) }
Slider(
    value = value,
    onValueChange = { value = it },
    colors = SliderDefaults.colors(
        thumbColor = MaterialTheme.colorScheme.tertiary,
        activeTrackColor = MaterialTheme.colorScheme.tertiary,
    ),
    modifier = Modifier.width(280.dp),
)

interactionSource: MutableInteractionSourceOptional

Default: remember { MutableInteractionSource() }

Lets you observe or control press, drag and focus states.

Custom thumb and track

Like basic, plus thumb and track to draw your own.

@Composable
fun Slider(
    value: Float,
    onValueChange: (Float) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    onValueChangeFinished: (() -> Unit)? = null,
    colors: SliderColors = SliderDefaults.colors(),
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    steps: Int = 0,
    thumb: @Composable (SliderState) -> Unit = { SliderDefaults.Thumb(interactionSource = interactionSource, colors = colors, enabled = enabled) },
    track: @Composable (SliderState) -> Unit = { sliderState -> SliderDefaults.Track(colors = colors, enabled = enabled, sliderState = sliderState) },
    valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
)

thumb: @Composable (SliderState) -> UnitOptional

Default: { SliderDefaults.Thumb(interactionSource = interactionSource, colors = colors, enabled = enabled) }

Draws the thumb. Default: SliderDefaults.Thumb.

track: @Composable (SliderState) -> UnitOptional

Default: { sliderState -> SliderDefaults.Track(colors = colors, enabled = enabled, sliderState = sliderState) }

Draws the track. Default: SliderDefaults.Track.

Same as above: value, onValueChange, modifier, enabled, onValueChangeFinished, colors, interactionSource, steps, valueRange.

State

Takes a SliderState that holds the value.

@Composable
fun Slider(
    state: SliderState,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    colors: SliderColors = SliderDefaults.colors(),
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    thumb: @Composable (SliderState) -> Unit = { SliderDefaults.Thumb(interactionSource = interactionSource, colors = colors, enabled = enabled) },
    track: @Composable (SliderState) -> Unit = { sliderState -> SliderDefaults.Track(colors = colors, enabled = enabled, sliderState = sliderState) },
)

state: SliderStateRequired

Holds the value, range and steps. Create it with remember { SliderState(value = 0.5f) }.

Same as above: modifier, enabled, colors, interactionSource, thumb, track.

Slider types

See also