Snackbar

A short message about a process that just happened, with an optional action.

When to use

Show a snackbar to confirm a result such as archived or sent, and offer a way to undo it. It appears at the bottom of the screen without interrupting what the user is doing.

Drive snackbars through a SnackbarHostState. showSnackbar suspends until the snackbar is dismissed and returns whether the action was used.

Playground

The interactive page has a playground with live property controls for this component and generated code.

Examples

With undo action

The result of the action is read from showSnackbar.

val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
var result by remember { mutableStateOf("") }

Box(modifier = Modifier.fillMaxWidth().height(220.dp)) {
    Button(
        onClick = {
            scope.launch {
                val outcome = snackbarHostState.showSnackbar(
                    message = "Message archived",
                    actionLabel = "Undo",
                    withDismissAction = true,
                )
                result = if (outcome == SnackbarResult.ActionPerformed) "Undo pressed" else ""
            }
        },
        modifier = Modifier.align(Alignment.TopStart),
    ) {
        Text("Show snackbar")
    }
    if (result.isNotEmpty()) {
        Text(result, modifier = Modifier.align(Alignment.TopEnd).padding(top = 12.dp))
    }
    SnackbarHost(hostState = snackbarHostState, modifier = Modifier.align(Alignment.BottomCenter))
}

Parameters

NameTypeDefaultDescription
messageStringText to show.
actionLabelString?nullLabel of the single action. Without one the snackbar is a plain message.
withDismissActionBooleanfalseAdds a close icon.
durationSnackbarDurationShort, or Indefinite with an actionHow long it stays.

Guidelines

Do:

Don't:

Accessibility

Related components