androidx.compose.material3

AlertDialog

Composable function

A dialog interrupts the user to ask for a decision, like confirming a delete. Use it only for important questions. For a short message, use a snackbar.

A dialog is shown while it is in the composition. Keep a Boolean state and wrap the dialog in if (showDialog) { ... }.

Confirm a destructive action
Confirm a destructive action
// The Box only gives the picture a size. In an app, show the dialog when a state is true.
Box(Modifier.size(360.dp, 340.dp)) {
    AlertDialog(
        onDismissRequest = {},
        title = { Text("Delete 3 photos?") },
        text = { Text("They will be removed from all your devices.") },
        confirmButton = { TextButton(onClick = {}) { Text("Delete") } },
        dismissButton = { TextButton(onClick = {}) { Text("Cancel") } },
    )
}

For a dialog with your own layout, use BasicAlertDialog. It gives you an empty dialog window; put a Surface inside.

Parameters

@Composable
fun AlertDialog(
    onDismissRequest: () -> Unit,
    confirmButton: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    dismissButton: @Composable (() -> Unit)? = null,
    icon: @Composable (() -> Unit)? = null,
    title: @Composable (() -> Unit)? = null,
    text: @Composable (() -> Unit)? = null,
    shape: Shape = AlertDialogDefaults.shape,
    containerColor: Color = AlertDialogDefaults.containerColor,
    iconContentColor: Color = AlertDialogDefaults.iconContentColor,
    titleContentColor: Color = AlertDialogDefaults.titleContentColor,
    textContentColor: Color = AlertDialogDefaults.textContentColor,
    tonalElevation: Dp = AlertDialogDefaults.TonalElevation,
    properties: DialogProperties = DialogProperties(),
)

onDismissRequest: () -> UnitRequired

Called when the user taps outside the dialog or presses back. Usually set your state to false here. The dialog does not close by itself.

confirmButton: @Composable () -> UnitRequired

The button that confirms, like "Delete" or "OK". Use a TextButton. Close the dialog in its onClick.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position of the dialog. See the Modifier guide.

dismissButton: @Composable (() -> Unit)?Optional

Default: null

The button that cancels, like "Cancel". Use a TextButton.

icon: @Composable (() -> Unit)?Optional

Default: null

An icon above the title. When set, the title is centered.

With an icon
With an icon
Box(Modifier.size(360.dp, 380.dp)) {
    AlertDialog(
        onDismissRequest = {},
        icon = { Icon(Icons.Filled.Delete, contentDescription = null) },
        title = { Text("Empty trash?") },
        text = { Text("Items in the trash will be deleted forever.") },
        confirmButton = { TextButton(onClick = {}) { Text("Empty") } },
        dismissButton = { TextButton(onClick = {}) { Text("Cancel") } },
    )
}

title: @Composable (() -> Unit)?Optional

Default: null

A short question or statement, like "Delete 3 photos?".

text: @Composable (() -> Unit)?Optional

Default: null

More detail about the decision. Keep it to one or two sentences.

shape: ShapeOptional

Default: AlertDialogDefaults.shape

Sets the outline. Default: the theme's extraLarge shape (28 dp corners).

containerColor: ColorOptional

Default: AlertDialogDefaults.containerColor

The background color of the dialog.

iconContentColor: ColorOptional

Default: AlertDialogDefaults.iconContentColor

The color of the icon.

titleContentColor: ColorOptional

Default: AlertDialogDefaults.titleContentColor

The color of the title.

textContentColor: ColorOptional

Default: AlertDialogDefaults.textContentColor

The color of the text.

tonalElevation: DpOptional

Default: AlertDialogDefaults.TonalElevation

Tints the background a little toward the primary color.

properties: DialogPropertiesOptional

Default: DialogProperties()

Window options, like whether tapping outside or pressing back closes the dialog. See DialogProperties.

Dialog types

See also