New Compose Multiplatform components arrived on Composables UICheck it out →

Component in Wear Material Compose

CompactChip

Android

A compact Wear Material Chip that offers two slots and a specific layout for an icon and label. Both the icon and label are optional however it is expected that at least one will be provided.

The [CompactChip] is Stadium shaped and has a max height designed to take no more than one line of text of [Typography.caption1] style and/or one icon. The default max height is [ChipDefaults.CompactChipHeight]. This includes a visible chip height of 32.dp and 8.dp of padding above and below the chip in order to meet accessibility guidelines that request a minimum of 48.dp height and width of tappable area.

If a icon is provided then the labels should be "start" aligned, e.g. left aligned in ltr so that the text starts next to the icon.

The items are laid out as follows.

  1. If a label is provided then the chip will be laid out with the optional icon at the start of a row followed by the label with a default max height of [ChipDefaults.CompactChipHeight].

  2. If only an icon is provided it will be laid out vertically and horizontally centered with a default height of [ChipDefaults.CompactChipHeight] and the default width of [ChipDefaults.IconOnlyCompactChipWidth]

If neither icon nor label is provided then the chip will displayed like an icon only chip but with no contents and [ChipColors.background()] color.

The [CompactChip] can have different styles with configurable content colors, background colors including gradients, these are provided by [ChipColors] implementations.

The recommended set of [ChipColors] styles can be obtained from [ChipDefaults], e.g. [ChipDefaults.primaryChipColors] to get a color scheme for a primary [Chip] which by default will have a solid background of [Colors.primary] and content color of [Colors.onPrimary].

Chips can be enabled or disabled. A disabled chip will not respond to click events.

Last updated:

Installation

dependencies {
   implementation("androidx.wear.compose:compose-material:1.4.0-beta03")
}

Overloads

@Composable
@Deprecated("This overload is provided for backwards compatibility with Compose for Wear OS 1.0." +
    "A newer overload is available with an additional shape parameter.",
    level = DeprecationLevel.HIDDEN
fun CompactChip(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    label: (@Composable RowScope.() -> Unit)? = null,
    icon: (@Composable BoxScope.() -> Unit)? = null,
    colors: ChipColors = ChipDefaults.primaryChipColors(),
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    contentPadding: PaddingValues = ChipDefaults.CompactChipContentPadding,
)

Parameters

namedescription
onClickWill be called when the user clicks the chip
modifierModifier to be applied to the chip
labelA slot for providing the chip's main label. The contents are expected to be text which is "start" aligned if there is an icon preset and "center" aligned if not.
iconA slot for providing the chip's icon. The contents are expected to be a horizontally and vertically aligned icon of size [ChipDefaults.SmallIconSize] when used with a label or [ChipDefaults.IconSize] when used as the only content in the CompactChip. In order to correctly render when the Chip is not enabled the icon must set its alpha value to [LocalContentAlpha].
colors[ChipColors] that will be used to resolve the background and content color for this chip in different states. See [ChipDefaults.chipColors]. Defaults to [ChipDefaults.primaryChipColors]
enabledControls the enabled state of the chip. When false, this chip will not be clickable
interactionSourceThe [MutableInteractionSource] representing the stream of [Interaction]s for this Chip. You can create and pass in your own remembered [MutableInteractionSource] if you want to observe [Interaction]s and customize the appearance / behavior of this Chip in different [Interaction]s.
contentPaddingThe spacing values to apply internally between the container and the content
@Composable
fun CompactChip(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    label: (@Composable RowScope.() -> Unit)? = null,
    icon: (@Composable BoxScope.() -> Unit)? = null,
    colors: ChipColors = ChipDefaults.primaryChipColors(),
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource? = null,
    contentPadding: PaddingValues = ChipDefaults.CompactChipContentPadding,
    shape: Shape = MaterialTheme.shapes.large,
    border: ChipBorder = ChipDefaults.chipBorder()
)

Parameters

namedescription
onClickWill be called when the user clicks the chip
modifierModifier to be applied to the chip
labelA slot for providing the chip's main label. The contents are expected to be text which is "start" aligned if there is an icon preset and "center" aligned if not.
iconA slot for providing the chip's icon. The contents are expected to be a horizontally and vertically aligned icon of size [ChipDefaults.SmallIconSize] when used with a label or [ChipDefaults.IconSize] when used as the only content in the CompactChip. In order to correctly render when the Chip is not enabled the icon must set its alpha value to [LocalContentAlpha].
colors[ChipColors] that will be used to resolve the background and content color for this chip in different states. See [ChipDefaults.chipColors]. Defaults to [ChipDefaults.primaryChipColors]
enabledControls the enabled state of the chip. When false, this chip will not be clickable
interactionSourcean optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this chip. You can use this to change the chip's appearance or preview the chip in different states. Note that if null is provided, interactions will still happen internally.
contentPaddingThe spacing values to apply internally between the container and the content
shapeDefines the chip's shape. It is strongly recommended to use the default as this shape is a key characteristic of the Wear Material Theme
border[ChipBorder] that will be used to resolve the border for this chip in different states. See [ChipDefaults.chipBorder].

Code Examples

CompactChipWithIconAndLabel

@Composable
@Sampled
fun CompactChipWithIconAndLabel() {
    CompactChip(
        onClick = { /* Do something */ },
        enabled = true,
        // CompactChip label should be no more than 1 line of text
        label = {
            Text("Single line label", maxLines = 1, overflow = TextOverflow.Ellipsis)
        },
        icon = {
            Icon(
                painter = painterResource(id = R.drawable.ic_airplanemode_active_24px),
                contentDescription = "airplane",
                modifier = Modifier.size(ChipDefaults.SmallIconSize),
            )
        },
    )
}

CompactChipWithLabel

@Composable
@Sampled
fun CompactChipWithLabel() {
    CompactChip(
        onClick = { /* Do something */ },
        enabled = true,
        // CompactChip label should be no more than 1 line of text
        label = {
            Text(
                text = "Single line label",
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                textAlign = TextAlign.Center,
                modifier = Modifier.fillMaxWidth()
            )
        },
    )
}

CompactChipWithIcon

@Composable
@Sampled
fun CompactChipWithIcon() {
    CompactChip(
        onClick = { /* Do something */ },
        enabled = true,
        icon = {
            Icon(
                painter = painterResource(id = R.drawable.ic_airplanemode_active_24px),
                contentDescription = "airplane",
                modifier = Modifier.size(ChipDefaults.IconSize)
            )
        },
    )
}