New Compose Multiplatform components arrived on Composables UICheck it out →

Component in Material Compose

AlertDialog

Android
Desktop

Alert dialogs interrupt users with urgent information, details, or actions.

Dialogs image

Last updated:

Installation

dependencies {
   implementation("androidx.compose.material:material:1.7.0-beta04")
}

Overloads

@Composable
fun AlertDialog(
    onDismissRequest: () -> Unit,
    confirmButton: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    dismissButton: @Composable (() -> Unit)? = null,
    title: @Composable (() -> Unit)? = null,
    text: @Composable (() -> Unit)? = null,
    shape: Shape = MaterialTheme.shapes.medium,
    backgroundColor: Color = MaterialTheme.colors.surface,
    contentColor: Color = contentColorFor(backgroundColor),
    properties: DialogProperties = DialogProperties()
)

Parameters

namedescription
onDismissRequestExecutes when the user tries to dismiss the Dialog by clicking outside or pressing the back button. This is not called when the dismiss button is clicked.
confirmButtonA button which is meant to confirm a proposed action, thus resolving what triggered the dialog. The dialog does not set up any events for this button so they need to be set up by the caller.
modifierModifier to be applied to the layout of the dialog.
dismissButtonA button which is meant to dismiss the dialog. The dialog does not set up any events for this button so they need to be set up by the caller.
titleThe title of the Dialog which should specify the purpose of the Dialog. The title is not mandatory, because there may be sufficient information inside the [text]. Provided text style will be [Typography.subtitle1].
textThe text which presents the details regarding the Dialog's purpose. Provided text style will be [Typography.body2].
shapeDefines the Dialog's shape
backgroundColorThe background color of the dialog.
contentColorThe preferred content color provided by this dialog to its children.
propertiesTypically platform specific properties to further configure the dialog.
@Composable
fun AlertDialog(
    onDismissRequest: () -> Unit,
    buttons: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    title: (@Composable () -> Unit)? = null,
    text: @Composable (() -> Unit)? = null,
    shape: Shape = MaterialTheme.shapes.medium,
    backgroundColor: Color = MaterialTheme.colors.surface,
    contentColor: Color = contentColorFor(backgroundColor),
    properties: DialogProperties = DialogProperties()
)

Parameters

namedescription
onDismissRequestExecutes when the user tries to dismiss the Dialog by clicking outside or pressing the back button. This is not called when the dismiss button is clicked.
buttonsFunction that emits the layout with the buttons.
modifierModifier to be applied to the layout of the dialog.
titleThe title of the Dialog which should specify the purpose of the Dialog. The title is not mandatory, because there may be sufficient information inside the [text]. Provided text style will be [Typography.subtitle1].
textThe text which presents the details regarding the Dialog's purpose. Provided text style will be [Typography.body2].
shapeDefines the Dialog's shape.
backgroundColorThe background color of the dialog.
contentColorThe preferred content color provided by this dialog to its children.
propertiesTypically platform specific properties to further configure the dialog.
@ExperimentalMaterialApi
@Composable
fun AlertDialog(
    onDismissRequest: () -> Unit,
    confirmButton: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    dismissButton: @Composable (() -> Unit)? = null,
    title: @Composable (() -> Unit)? = null,
    text: @Composable (() -> Unit)? = null,
    shape: Shape = MaterialTheme.shapes.medium,
    backgroundColor: Color = MaterialTheme.colors.surface,
    contentColor: Color = contentColorFor(backgroundColor),
    dialogProvider: AlertDialogProvider = PopupAlertDialogProvider
)

Parameters

namedescription
onDismissRequestCallback that will be called when the user closes the dialog.
confirmButtonA button which is meant to confirm a proposed action, thus resolving what triggered the dialog. The dialog does not set up any events for this button so they need to be set up by the caller.
modifierModifier to be applied to the layout of the dialog.
dismissButtonA button which is meant to dismiss the dialog. The dialog does not set up any events for this button so they need to be set up by the caller.
titleThe title of the Dialog which should specify the purpose of the Dialog. The title is not mandatory, because there may be sufficient information inside the [text]. Provided text style will be [Typography.subtitle1].
textThe text which presents the details regarding the Dialog's purpose. Provided text style will be [Typography.body2].
shapeDefines the Dialog's shape
backgroundColorThe background color of the dialog.
contentColorThe preferred content color provided by this dialog to its children.
dialogProviderDefines how to create dialog in which will be placed AlertDialog's content.
@ExperimentalMaterialApi
@Composable
fun AlertDialog(
    onDismissRequest: () -> Unit,
    buttons: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    title: (@Composable () -> Unit)? = null,
    text: @Composable (() -> Unit)? = null,
    shape: Shape = MaterialTheme.shapes.medium,
    backgroundColor: Color = MaterialTheme.colors.surface,
    contentColor: Color = contentColorFor(backgroundColor),
    dialogProvider: AlertDialogProvider = PopupAlertDialogProvider
)

Parameters

namedescription
onDismissRequestCallback that will be called when the user closes the dialog.
buttonsFunction that emits the layout with the buttons.
modifierModifier to be applied to the layout of the dialog.
titleThe title of the Dialog which should specify the purpose of the Dialog. The title is not mandatory, because there may be sufficient information inside the [text]. Provided text style will be [Typography.subtitle1].
textThe text which presents the details regarding the Dialog's purpose. Provided text style will be [Typography.body2].
shapeDefines the Dialog's shape.
backgroundColorThe background color of the dialog.
contentColorThe preferred content color provided by this dialog to its children.
dialogProviderDefines how to create dialog in which will be placed AlertDialog's content.

Code Examples

AlertDialogSample

@Composable
@Sampled
fun AlertDialogSample() {
    val openDialog = remember { mutableStateOf(true) }

    if (openDialog.value) {
        AlertDialog(
            onDismissRequest = {
                // Dismiss the dialog when the user clicks outside the dialog or on the back
                // button. If you want to disable that functionality, simply use an empty
                // onCloseRequest.
                openDialog.value = false
            },
            title = {
                Text(text = "Title")
            },
            text = {
                Text(
                    "This area typically contains the supportive text " +
                        "which presents the details regarding the Dialog's purpose."
                )
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        openDialog.value = false
                    }
                ) {
                    Text("Confirm")
                }
            },
            dismissButton = {
                TextButton(
                    onClick = {
                        openDialog.value = false
                    }
                ) {
                    Text("Dismiss")
                }
            }
        )
    }
}

CustomAlertDialogSample

@Composable
@Sampled
fun CustomAlertDialogSample() {
    val openDialog = remember { mutableStateOf(true) }

    if (openDialog.value) {
        AlertDialog(
            onDismissRequest = {
                openDialog.value = false
            },
            title = {
                Text(text = "Title")
            },
            text = {
                Text(
                    "This area typically contains the supportive text " +
                        "which presents the details regarding the Dialog's purpose."
                )
            },
            buttons = {
                Row(
                    modifier = Modifier.padding(all = 8.dp),
                    horizontalArrangement = Arrangement.Center
                ) {
                    Button(
                        modifier = Modifier.fillMaxWidth(),
                        onClick = { openDialog.value = false }
                    ) {
                        Text("Dismiss")
                    }
                }
            }
        )
    }
}