{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

AndroidAlertDialogKt

public final class AndroidAlertDialogKt


Summary

Public methods

static final void
@ExperimentalMaterial3Api
@Composable
AlertDialog(
    @NonNull Function0<Unit> onDismissRequest,
    @NonNull Modifier modifier,
    @NonNull DialogProperties properties,
    @Composable @NonNull Function0<Unit> content
)

This method is deprecated. Use BasicAlertDialog instead

static final void
@Composable
AlertDialog(
    @NonNull Function0<Unit> onDismissRequest,
    @Composable @NonNull Function0<Unit> confirmButton,
    @NonNull Modifier modifier,
    @Composable Function0<Unit> dismissButton,
    @Composable Function0<Unit> icon,
    @Composable Function0<Unit> title,
    @Composable Function0<Unit> text,
    @NonNull Shape shape,
    @NonNull Color containerColor,
    @NonNull Color iconContentColor,
    @NonNull Color titleContentColor,
    @NonNull Color textContentColor,
    @NonNull Dp tonalElevation,
    @NonNull DialogProperties properties
)

Material Design basic dialog.

static final void
@ExperimentalMaterial3Api
@Composable
BasicAlertDialog(
    @NonNull Function0<Unit> onDismissRequest,
    @NonNull Modifier modifier,
    @NonNull DialogProperties properties,
    @Composable @NonNull Function0<Unit> content
)

Basic alert dialog dialog.

Public methods

AlertDialog

@ExperimentalMaterial3Api
@Composable
public static final void AlertDialog(
    @NonNull Function0<Unit> onDismissRequest,
    @NonNull Modifier modifier,
    @NonNull DialogProperties properties,
    @Composable @NonNull Function0<Unit> content
)

Basic alert dialog dialog.

Dialogs provide important prompts in a user flow. They can require an action, communicate information, or help users accomplish a task.

Basic dialog image

This basic alert dialog expects an arbitrary content that is defined by the caller. Note that your content will need to define its own styling.

By default, the displayed dialog has the minimum height and width that the Material Design spec defines. If required, these constraints can be overwritten by providing a width or height Modifiers.

Basic alert dialog usage with custom content:

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.AlertDialogDefaults
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

val openDialog = remember { mutableStateOf(true) }

if (openDialog.value) {
    BasicAlertDialog(
        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
            // onDismissRequest.
            openDialog.value = false
        }
    ) {
        Surface(
            modifier = Modifier
                .wrapContentWidth()
                .wrapContentHeight(),
            shape = MaterialTheme.shapes.large,
            tonalElevation = AlertDialogDefaults.TonalElevation
        ) {
            Column(modifier = Modifier.padding(16.dp)) {
                Text(
                    text = "This area typically contains the supportive text " +
                        "which presents the details regarding the Dialog's purpose.",
                )
                Spacer(modifier = Modifier.height(24.dp))
                TextButton(
                    onClick = {
                        openDialog.value = false
                    },
                    modifier = Modifier.align(Alignment.End)
                ) {
                    Text("Confirm")
                }
            }
        }
    }
}
Parameters
@NonNull Function0<Unit> onDismissRequest

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

@NonNull Modifier modifier

the Modifier to be applied to this dialog's content.

@NonNull DialogProperties properties

typically platform specific properties to further configure the dialog.

@Composable @NonNull Function0<Unit> content

the content of the dialog

AlertDialog

@Composable
public static final void AlertDialog(
    @NonNull Function0<Unit> onDismissRequest,
    @Composable @NonNull Function0<Unit> confirmButton,
    @NonNull Modifier modifier,
    @Composable Function0<Unit> dismissButton,
    @Composable Function0<Unit> icon,
    @Composable Function0<Unit> title,
    @Composable Function0<Unit> text,
    @NonNull Shape shape,
    @NonNull Color containerColor,
    @NonNull Color iconContentColor,
    @NonNull Color titleContentColor,
    @NonNull Color textContentColor,
    @NonNull Dp tonalElevation,
    @NonNull DialogProperties properties
)

Material Design basic dialog.

Dialogs provide important prompts in a user flow. They can require an action, communicate information, or help users accomplish a task.

Basic dialog image

The dialog will position its buttons, typically TextButtons, 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.

Simple usage:

import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.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
            // onDismissRequest.
            openDialog.value = false
        },
        title = {
            Text(text = "Title")
        },
        text = {
            Text(text = "Turned on by default")
        },
        confirmButton = {
            TextButton(
                onClick = {
                    openDialog.value = false
                }
            ) {
                Text("Confirm")
            }
        },
        dismissButton = {
            TextButton(
                onClick = {
                    openDialog.value = false
                }
            ) {
                Text("Dismiss")
            }
        }
    )
}

Usage with a "Hero" icon:

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.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
            // onDismissRequest.
            openDialog.value = false
        },
        icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
        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
@NonNull Function0<Unit> onDismissRequest

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

@Composable @NonNull Function0<Unit> confirmButton

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.

@NonNull Modifier modifier

the Modifier to be applied to this dialog

@Composable Function0<Unit> dismissButton

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.

@Composable Function0<Unit> icon

optional icon that will appear above the title or above the text, in case a title was not provided.

@Composable Function0<Unit> title

title which should specify the purpose of the dialog. The title is not mandatory, because there may be sufficient information inside the text.

@Composable Function0<Unit> text

text which presents the details regarding the dialog's purpose.

@NonNull Shape shape

defines the shape of this dialog's container

@NonNull Color containerColor

the color used for the background of this dialog. Use Color.Transparent to have no color.

@NonNull Color iconContentColor

the content color used for the icon.

@NonNull Color titleContentColor

the content color used for the title.

@NonNull Color textContentColor

the content color used for the text.

@NonNull Dp tonalElevation

when containerColor is ColorScheme.surface, a translucent primary color overlay is applied on top of the container. A higher tonal elevation value will result in a darker color in light theme and lighter color in dark theme. See also: Surface.

@NonNull DialogProperties properties

typically platform specific properties to further configure the dialog.

See also
BasicAlertDialog

BasicAlertDialog

@ExperimentalMaterial3Api
@Composable
public static final void BasicAlertDialog(
    @NonNull Function0<Unit> onDismissRequest,
    @NonNull Modifier modifier,
    @NonNull DialogProperties properties,
    @Composable @NonNull Function0<Unit> content
)

Basic alert dialog dialog.

Dialogs provide important prompts in a user flow. They can require an action, communicate information, or help users accomplish a task.

Basic dialog image

This basic alert dialog expects an arbitrary content that is defined by the caller. Note that your content will need to define its own styling.

By default, the displayed dialog has the minimum height and width that the Material Design spec defines. If required, these constraints can be overwritten by providing a width or height Modifiers.

Basic alert dialog usage with custom content:

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.AlertDialogDefaults
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

val openDialog = remember { mutableStateOf(true) }

if (openDialog.value) {
    BasicAlertDialog(
        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
            // onDismissRequest.
            openDialog.value = false
        }
    ) {
        Surface(
            modifier = Modifier
                .wrapContentWidth()
                .wrapContentHeight(),
            shape = MaterialTheme.shapes.large,
            tonalElevation = AlertDialogDefaults.TonalElevation
        ) {
            Column(modifier = Modifier.padding(16.dp)) {
                Text(
                    text = "This area typically contains the supportive text " +
                        "which presents the details regarding the Dialog's purpose.",
                )
                Spacer(modifier = Modifier.height(24.dp))
                TextButton(
                    onClick = {
                        openDialog.value = false
                    },
                    modifier = Modifier.align(Alignment.End)
                ) {
                    Text("Confirm")
                }
            }
        }
    }
}
Parameters
@NonNull Function0<Unit> onDismissRequest

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

@NonNull Modifier modifier

the Modifier to be applied to this dialog's content.

@NonNull DialogProperties properties

typically platform specific properties to further configure the dialog.

@Composable @NonNull Function0<Unit> content

the content of the dialog