AlertDialog

Functions summary

Unit
@Composable
AlertDialog(
    onDismissRequest: () -> Unit,
    confirmButton: @Composable () -> Unit,
    modifier: Modifier,
    dismissButton: (@Composable () -> Unit)?,
    icon: (@Composable () -> Unit)?,
    title: (@Composable () -> Unit)?,
    text: (@Composable () -> Unit)?,
    containerShape: Shape,
    containerColor: Color,
    containerContentColor: Color,
    containerContentPadding: PaddingValues
)

Displays an urgent prompt requiring the user to acknowledge or take an action.

Functions

AlertDialog

@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,
    containerShape: Shape = AlertDialogDefaults.containerShape,
    containerColor: Color = AlertDialogDefaults.containerColor,
    containerContentColor: Color = AlertDialogDefaults.containerContentColor(containerColor),
    containerContentPadding: PaddingValues = AlertDialogDefaults.containerContentPadding
): Unit

Displays an urgent prompt requiring the user to acknowledge or take an action.

Alert dialogs interrupt the user to communicate critical information, confirm decisions (such as discarding changes or resetting preferences), or acknowledge important system status. The dialog dims the background to focus attention on the prompt and displays actions directly below the message.

The dialog container is the surface that holds the icon, title, and text. The confirmButton and dismissButton sit outside the container.

An alert dialog with confirm and dismiss actions:

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.xr.glimmer.AlertDialog
import androidx.xr.glimmer.Button
import androidx.xr.glimmer.Icon
import androidx.xr.glimmer.Text

var showDialog by remember { mutableStateOf(true) }

if (showDialog) {
    AlertDialog(
        onDismissRequest = { showDialog = false },
        confirmButton = { Button(onClick = { /* confirm action */ }) { Text("Confirm") } },
        dismissButton = { Button(onClick = { showDialog = false }) { Text("Dismiss") } },
        icon = { Icon(imageVector = FavoriteIcon, contentDescription = "Alert icon") },
        title = { Text("Title") },
        text = { Text("This is an alert dialog with options to confirm or dismiss.") },
    )
}

An alert dialog with only a confirm action:

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.xr.glimmer.AlertDialog
import androidx.xr.glimmer.Button
import androidx.xr.glimmer.Icon
import androidx.xr.glimmer.Text

var showDialog by remember { mutableStateOf(true) }

if (showDialog) {
    AlertDialog(
        onDismissRequest = { showDialog = false },
        confirmButton = { Button(onClick = { showDialog = false }) { Text("OK") } },
        icon = {
            Icon(
                imageVector = OutlinedFavoriteIcon,
                contentDescription = "Favorite icon",
            )
        },
        title = { Text("Title") },
        text = { Text("This is an alert dialog with only an option to dismiss.") },
    )
}
Parameters
onDismissRequest: () -> Unit

called when the user tries to dismiss the dialog

confirmButton: @Composable () -> Unit

button used to confirm or accept the action. This will be the first button shown and takes focus upon the dialog being opened. This button is placed outside the dialog container, below it, so containerShape, containerColor, containerContentColor, and containerContentPadding do not apply to it

modifier: Modifier = Modifier

the Modifier to be applied to the outer layout containing the dialog container, confirmButton, and dismissButton

dismissButton: (@Composable () -> Unit)? = null

optional button used to dismiss or cancel the alert dialog. This button is placed outside the dialog container, next to confirmButton, so containerShape, containerColor, containerContentColor, and containerContentPadding do not apply to it

icon: (@Composable () -> Unit)? = null

optional icon displayed at the top of the dialog, above the title

title: (@Composable () -> Unit)? = null

optional title displaying the primary headline of the dialog, placed below the icon and above the text

text: (@Composable () -> Unit)? = null

optional body text providing additional details or explanation for the dialog, placed below the title

containerShape: Shape = AlertDialogDefaults.containerShape

the Shape of the dialog container

containerColor: Color = AlertDialogDefaults.containerColor

the background Color of the dialog container

containerContentColor: Color = AlertDialogDefaults.containerContentColor(containerColor)

the default Color for the icon, title, and text inside the dialog container

containerContentPadding: PaddingValues = AlertDialogDefaults.containerContentPadding

the spacing values to apply internally between the dialog container and its content