Skip to the workspace
Pebellslearn by building
Navigation and App Structure  ›  Lesson

Dialogs and Bottom Sheets

Dialog 14 minIn Compose a dialog or bottom sheet is shown while a piece of state says so, and dismissing it means setting that state back.

android / Dialogs and Bottom Sheets

Explore each scene at your own pace.

01 · The rule

The rule

Dialogs and Bottom Sheets

Deleting a plant in Sprout should ask "Are you sure?" first. And choosing a watering schedule would be neater in a panel that slides up from the bottom. Both are overlays, and in Compose both follow the same rule, which surprises people coming from older Android code: there is no show() call.

The rule

A dialog is on screen while its state is true; dismiss means set it false.

Hold a boolean in state and wrap the dialog in an if:

var showDelete by remember { mutableStateOf(false) }

IconButton(onClick = { showDelete = true }) {
    Icon(Icons.Filled.Delete, contentDescription = "Delete plant")
}

if (showDelete) {
    AlertDialog(
        onDismissRequest = { showDelete = false },
        title = { Text("Delete Monstera?") },
        text = { Text("Its watering history will be lost.") },
        confirmButton = {
            TextButton(onClick = { onDelete(); showDelete = false }) { Text("Delete") }
        },
        dismissButton = {
            TextButton(onClick = { showDelete = false }) { Text("Cancel") }
        }
    )
}

onDismissRequest is called when the user taps outside or presses Back. It is only a request: the dialog closes because you set the state to false. Leave it empty and the dialog cannot be dismissed that way.

A ModalBottomSheet works the same way: show it inside if (showSheet) and set the flag false in its onDismissRequest. Choose by purpose:

  • AlertDialog — a short decision that interrupts: confirm, discard, allow.
  • Bottom sheet — extra content or a list of options tied to the current screen, such as picking a watering schedule.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.