AlertDialog

Functions summary

Unit
@Composable
AlertDialog(
    onDismissRequest: () -> Unit,
    confirmButton: @Composable () -> Unit,
    text: @Composable () -> Unit,
    modifier: Modifier,
    dismissButton: (@Composable () -> Unit)?,
    icon: (@Composable () -> Unit)?,
    title: (@Composable () -> Unit)?,
    shape: Shape,
    containerColor: Color,
    contentColor: Color,
    contentPadding: PaddingValues
)

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

Functions

AlertDialog

@Composable
fun AlertDialog(
    onDismissRequest: () -> Unit,
    confirmButton: @Composable () -> Unit,
    text: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    dismissButton: (@Composable () -> Unit)? = null,
    icon: (@Composable () -> Unit)? = null,
    title: (@Composable () -> Unit)? = null,
    shape: Shape = AlertDialogDefaults.shape,
    containerColor: Color = AlertDialogDefaults.containerColor,
    contentColor: Color = AlertDialogDefaults.contentColor(containerColor),
    contentPadding: PaddingValues = AlertDialogDefaults.contentPadding
): 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.

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

text: @Composable () -> Unit

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

modifier: Modifier = Modifier

the Modifier to be applied to the dialog

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

optional button used to dismiss or cancel the alert dialog

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

shape: Shape = AlertDialogDefaults.shape

the Shape of the dialog container

containerColor: Color = AlertDialogDefaults.containerColor

the background Color of the dialog container

contentColor: Color = AlertDialogDefaults.contentColor(containerColor)

the default Color for content inside the dialog container

contentPadding: PaddingValues = AlertDialogDefaults.contentPadding

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