AlertDialog

Functions summary

Unit
@Composable
AlertDialog(
    onDismissRequest: () -> Unit,
    buttons: @Composable () -> Unit,
    modifier: Modifier,
    title: (@Composable () -> Unit)?,
    text: (@Composable () -> Unit)?,
    shape: Shape,
    backgroundColor: Color,
    contentColor: Color,
    properties: DialogProperties
)

Material Design alert dialog

Cmn
android
Unit
@Composable
AlertDialog(
    onDismissRequest: () -> Unit,
    confirmButton: @Composable () -> Unit,
    modifier: Modifier,
    dismissButton: (@Composable () -> Unit)?,
    title: (@Composable () -> Unit)?,
    text: (@Composable () -> Unit)?,
    shape: Shape,
    backgroundColor: Color,
    contentColor: Color,
    properties: DialogProperties
)

Material Design alert dialog

Cmn
android

Functions

AlertDialog

@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()
): Unit

Material Design alert dialog

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

Dialogs
image

This function can be used to fully customize the button area, e.g. with:

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

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")
                }
            }
        },
    )
}
Parameters
onDismissRequest: () -> Unit

Executes 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.

buttons: @Composable () -> Unit

Function that emits the layout with the buttons.

modifier: Modifier = Modifier

Modifier to be applied to the layout of the dialog.

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

The 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.

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

The text which presents the details regarding the Dialog's purpose. Provided text style will be Typography.body2.

shape: Shape = MaterialTheme.shapes.medium

Defines the Dialog's shape.

backgroundColor: Color = MaterialTheme.colors.surface

The background color of the dialog.

contentColor: Color = contentColorFor(backgroundColor)

The preferred content color provided by this dialog to its children.

properties: DialogProperties = DialogProperties()

Typically platform specific properties to further configure the dialog.

AlertDialog

@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()
): Unit

Material Design alert dialog

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

Dialogs
image

The dialog will position its buttons based on the available space. By default it will try to place them horizontally next to each other and fallback to horizontal placement if not enough space is available. There is also another version of this composable that has a slot for buttons to provide custom buttons layout.

Sample of dialog:

import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

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") }
        },
    )
}
Parameters
onDismissRequest: () -> Unit

Executes 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.

confirmButton: @Composable () -> Unit

A 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.

modifier: Modifier = Modifier

Modifier to be applied to the layout of the dialog.

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

A 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.

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

The 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.

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

The text which presents the details regarding the Dialog's purpose. Provided text style will be Typography.body2.

shape: Shape = MaterialTheme.shapes.medium

Defines the Dialog's shape

backgroundColor: Color = MaterialTheme.colors.surface

The background color of the dialog.

contentColor: Color = contentColorFor(backgroundColor)

The preferred content color provided by this dialog to its children.

properties: DialogProperties = DialogProperties()

Typically platform specific properties to further configure the dialog.