Label
Common
Component in Material 3 Compose
Label component that will append a [label] to [content]. The positioning logic uses [TooltipDefaults.rememberPlainTooltipPositionProvider].
Label appended to thumbs of Slider:
@sample androidx.compose.material3.samples.SliderWithCustomThumbSample
Last updated:
Installation
dependencies {
implementation("androidx.compose.material3:material3:1.4.0-alpha02")
}
Overloads
@ExperimentalMaterial3Api
@Composable
fun Label(
label: @Composable TooltipScope.() -> Unit,
modifier: Modifier = Modifier,
interactionSource: MutableInteractionSource? = null,
isPersistent: Boolean = false,
content: @Composable () -> Unit
)
Parameters
name | description |
---|---|
label | composable that will be appended to [content] |
modifier | [Modifier] that will be applied to [content] |
interactionSource | the [MutableInteractionSource] representing the stream of [Interaction]s for the [content]. |
isPersistent | boolean to determine if the label should be persistent. If true, then the label will always show and be anchored to [content]. if false, then the label will only show when pressing down or hovering over the [content]. |
content | the composable that [label] will anchor to. |
Code Examples
SliderWithCustomThumbSample
@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun SliderWithCustomThumbSample() {
var sliderPosition by remember { mutableStateOf(0f) }
val interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
Column(modifier = Modifier.padding(horizontal = 16.dp)) {
Slider(
modifier = Modifier.semantics { contentDescription = "Localized Description" },
value = sliderPosition,
onValueChange = { sliderPosition = it },
valueRange = 0f..100f,
interactionSource = interactionSource,
onValueChangeFinished = {
// launch some business logic update with the state you hold
// viewModel.updateSelectedSliderValue(sliderPosition)
},
thumb = {
Label(
label = {
PlainTooltip(modifier = Modifier.sizeIn(45.dp, 25.dp).wrapContentWidth()) {
Text("%.2f".format(sliderPosition))
}
},
interactionSource = interactionSource
) {
Icon(
imageVector = Icons.Filled.Favorite,
contentDescription = null,
modifier = Modifier.size(ButtonDefaults.IconSize),
tint = Color.Red
)
}
}
)
}
}
RangeSliderWithCustomComponents
@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun RangeSliderWithCustomComponents() {
val rangeSliderState = remember {
RangeSliderState(
0f,
100f,
valueRange = 0f..100f,
onValueChangeFinished = {
// launch some business logic update with the state you hold
// viewModel.updateSelectedSliderValue(sliderPosition)
}
)
}
val startInteractionSource = remember { MutableInteractionSource() }
val endInteractionSource = remember { MutableInteractionSource() }
val startThumbAndTrackColors =
SliderDefaults.colors(thumbColor = Color.Blue, activeTrackColor = Color.Red)
val endThumbColors = SliderDefaults.colors(thumbColor = Color.Green)
Column(modifier = Modifier.padding(horizontal = 16.dp)) {
RangeSlider(
state = rangeSliderState,
modifier = Modifier.semantics { contentDescription = "Localized Description" },
startInteractionSource = startInteractionSource,
endInteractionSource = endInteractionSource,
startThumb = {
Label(
label = {
PlainTooltip(modifier = Modifier.sizeIn(45.dp, 25.dp).wrapContentWidth()) {
Text("%.2f".format(rangeSliderState.activeRangeStart))
}
},
interactionSource = startInteractionSource
) {
SliderDefaults.Thumb(
interactionSource = startInteractionSource,
colors = startThumbAndTrackColors
)
}
},
endThumb = {
Label(
label = {
PlainTooltip(
modifier = Modifier.requiredSize(45.dp, 25.dp).wrapContentWidth()
) {
Text("%.2f".format(rangeSliderState.activeRangeEnd))
}
},
interactionSource = endInteractionSource
) {
SliderDefaults.Thumb(
interactionSource = endInteractionSource,
colors = endThumbColors
)
}
},
track = { rangeSliderState ->
SliderDefaults.Track(
colors = startThumbAndTrackColors,
rangeSliderState = rangeSliderState
)
}
)
}
}