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) { ... }.

// 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
onDismissRequest: () -> UnitRequiredCalled 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
confirmButton: @Composable () -> UnitRequiredThe button that confirms, like "Delete" or "OK". Use a TextButton.
Close the dialog in its onClick.
modifier: ModifierOptional
modifier: ModifierOptionalDefault: Modifier
Changes size, padding, or position of the dialog. See the Modifier guide.
dismissButton: @Composable (() -> Unit)?Optional
dismissButton: @Composable (() -> Unit)?OptionalDefault: null
The button that cancels, like "Cancel". Use a TextButton.
icon: @Composable (() -> Unit)?Optional
icon: @Composable (() -> Unit)?OptionalDefault: null
An icon above the title. When set, the title is centered.

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
title: @Composable (() -> Unit)?OptionalDefault: null
A short question or statement, like "Delete 3 photos?".
text: @Composable (() -> Unit)?Optional
text: @Composable (() -> Unit)?OptionalDefault: null
More detail about the decision. Keep it to one or two sentences.
shape: ShapeOptional
shape: ShapeOptionalDefault: AlertDialogDefaults.shape
Sets the outline. Default: the theme's extraLarge shape (28 dp corners).
containerColor: ColorOptional
containerColor: ColorOptionalDefault: AlertDialogDefaults.containerColor
The background color of the dialog.
iconContentColor: ColorOptional
iconContentColor: ColorOptionalDefault: AlertDialogDefaults.iconContentColor
The color of the icon.
titleContentColor: ColorOptional
titleContentColor: ColorOptionalDefault: AlertDialogDefaults.titleContentColor
The color of the title.
textContentColor: ColorOptional
textContentColor: ColorOptionalDefault: AlertDialogDefaults.textContentColor
The color of the text.
tonalElevation: DpOptional
tonalElevation: DpOptionalDefault: AlertDialogDefaults.TonalElevation
Tints the background a little toward the primary color.
properties: DialogPropertiesOptional
properties: DialogPropertiesOptionalDefault: DialogProperties()
Window options, like whether tapping outside or pressing back closes the dialog. See DialogProperties.
Dialog types
AlertDialogBasicAlertDialog
See also
AlertDialogDefaults: default values and helper functions forAlertDialog.