IconToggleButton
Common
Component in Material 3 Compose
Icon buttons help people take supplementary actions with a single tap. They’re used when a compact button is required, such as in a toolbar or image list.
Last updated:
Installation
dependencies {
implementation("androidx.compose.material3:material3:1.4.0-alpha02")
}
Overloads
@Deprecated(
message = "Use overload with `shape`",
replaceWith =
ReplaceWith(
"IconToggleButton(checked, onCheckedChange, modifier, enabled, colors," +
" interactionSource, shape, content)"
),
level = DeprecationLevel.HIDDEN
)
@Composable
fun IconToggleButton(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
colors: IconToggleButtonColors = IconButtonDefaults.iconToggleButtonLocalContentColors(),
interactionSource: MutableInteractionSource? = null,
content: @Composable () -> Unit
)
Parameters
name | description |
---|---|
checked | whether this icon button is toggled on or off |
onCheckedChange | called when this icon button is clicked |
modifier | the [Modifier] to be applied to this icon button |
enabled | controls the enabled state of this icon button. When false , this component will not respond to user input, and it will appear visually disabled and disabled to accessibility services. |
colors | [IconToggleButtonColors] that will be used to resolve the colors used for this icon button in different states. See [IconButtonDefaults.iconToggleButtonColors]. |
interactionSource | an optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this icon button. You can use this to change the icon button's appearance or preview the icon button in different states. Note that if null is provided, interactions will still happen internally. |
content | the content of this icon button, typically an [Icon] |
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
fun IconToggleButton(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
colors: IconToggleButtonColors = IconButtonDefaults.iconToggleButtonLocalContentColors(),
interactionSource: MutableInteractionSource? = null,
shape: Shape = IconButtonDefaults.standardShape,
content: @Composable () -> Unit
)
Parameters
name | description |
---|---|
checked | whether this icon button is toggled on or off |
onCheckedChange | called when this icon button is clicked |
modifier | the [Modifier] to be applied to this icon button |
enabled | controls the enabled state of this icon button. When false , this component will not respond to user input, and it will appear visually disabled and disabled to accessibility services. |
colors | [IconToggleButtonColors] that will be used to resolve the colors used for this icon button in different states. See [IconButtonDefaults.iconToggleButtonColors] and [IconButtonDefaults.iconToggleButtonLocalContentColors]. |
interactionSource | an optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this icon button. You can use this to change the icon button's appearance or preview the icon button in different states. Note that if null is provided, interactions will still happen internally. |
shape | the [Shape] of this icon button. |
content | the content of this icon button, typically an [Icon] |
@ExperimentalMaterial3ExpressiveApi
@Composable
fun IconToggleButton(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
shapes: IconButtonShapes,
modifier: Modifier = Modifier,
enabled: Boolean = true,
colors: IconToggleButtonColors = IconButtonDefaults.iconToggleButtonColors(),
interactionSource: MutableInteractionSource? = null,
content: @Composable () -> Unit
)
Parameters
name | description |
---|---|
checked | whether this icon button is toggled on or off |
onCheckedChange | called when this icon button is clicked |
shapes | the [IconButtonShapes] that the icon toggle button will morph between depending on the user's interaction with the icon toggle button. |
modifier | the [Modifier] to be applied to this icon button |
enabled | controls the enabled state of this icon button. When false , this component will not respond to user input, and it will appear visually disabled and disabled to accessibility services. |
colors | [IconToggleButtonColors] that will be used to resolve the colors used for this icon button in different states. See [IconButtonDefaults.iconToggleButtonColors]. |
interactionSource | an optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this icon button. You can use this to change the icon button's appearance or preview the icon button in different states. Note that if null is provided, interactions will still happen internally. |
content | the content of this icon button, typically an [Icon] |
Code Examples
IconToggleButtonSample
@Preview
@Composable
fun IconToggleButtonSample() {
var checked by remember { mutableStateOf(false) }
IconToggleButton(checked = checked, onCheckedChange = { checked = it }) {
if (checked) {
Icon(Icons.Filled.Lock, contentDescription = "Localized description")
} else {
Icon(Icons.Outlined.Lock, contentDescription = "Localized description")
}
}
}
IconToggleButtonWithAnimatedShapeSample
@Preview
@Composable
fun IconToggleButtonWithAnimatedShapeSample() {
var checked by remember { mutableStateOf(false) }
IconToggleButton(checked = checked, onCheckedChange = { checked = it }) {
if (checked) {
Icon(Icons.Filled.Lock, contentDescription = "Localized description")
} else {
Icon(Icons.Outlined.Lock, contentDescription = "Localized description")
}
}
}