Compose Unstyled 2.0 is out! Check the official announcement blog ->

Alert Dialog

Modal dialogs for confirmations, destructive actions, and focused decisions.

Use alert dialogs when you need to pause the current flow and ask for a clear choice.

View on GitHub
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import com.composables.ui.components.AlertDialog
import com.composables.ui.components.Button
import com.composables.ui.components.ButtonStyle
import com.composables.ui.components.Text

@Composable
fun AlertDialogExample() {
    var visible by remember { mutableStateOf(false) }
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
    ) {
        Button(onClick = { visible = true }) {
            Text(text = "Show dialog")
        }

        AlertDialog(
            visible = visible,
            onDismissRequest = { visible = false },
            title = { Text("Enable notifications?", textAlign = TextAlign.Center) },
            text = {
                Text(
                    "Notifications help you keep up with important updates from this app.",
                    textAlign = TextAlign.Center,
                )
            },
            positiveButton = {
                Button(
                    onClick = { visible = false },
                    modifier = Modifier.fillMaxWidth(),
                    style = ButtonStyle.Primary,
                ) {
                    Text("Allow")
                }
            },
            negativeButton = {
                Button(
                    onClick = { visible = false },
                    modifier = Modifier.fillMaxWidth(),
                    style = ButtonStyle.Secondary,
                ) {
                    Text("Not now")
                }
            },
        )
    }
}

Installation

implementation("com.composables:ui:0.1.0")

Examples

Three actions

View on GitHub
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import com.composables.ui.components.AlertDialog
import com.composables.ui.components.Button
import com.composables.ui.components.ButtonStyle
import com.composables.ui.components.Text

@Composable
fun AlertDialogThreeActionsExample() {
    var visible by remember { mutableStateOf(false) }
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
    ) {
        Button(onClick = { visible = true }) {
            Text(text = "Show dialog")
        }

        AlertDialog(
            visible = visible,
            onDismissRequest = { visible = false },
            title = { Text("Save changes?", textAlign = TextAlign.Center) },
            text = {
                Text(
                    "You can save your edits, keep working, or discard the changes.",
                    textAlign = TextAlign.Center,
                )
            },
            positiveButton = {
                Button(
                    onClick = { visible = false },
                    modifier = Modifier.fillMaxWidth(),
                    style = ButtonStyle.Primary,
                ) {
                    Text("Save")
                }
            },
            neutralButton = {
                Button(
                    onClick = { visible = false },
                    modifier = Modifier.fillMaxWidth(),
                    style = ButtonStyle.Secondary,
                ) {
                    Text("Keep editing")
                }
            },
            negativeButton = {
                Button(
                    onClick = { visible = false },
                    modifier = Modifier.fillMaxWidth(),
                    style = ButtonStyle.Secondary,
                ) {
                    Text("Discard")
                }
            },
        )
    }
}

With an icon

View on GitHub
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.composables.icons.lucide.Bell
import com.composables.icons.lucide.Lucide
import com.composables.ui.components.AlertDialog
import com.composables.ui.components.Button
import com.composables.ui.components.ButtonStyle
import com.composables.ui.components.Icon
import com.composables.ui.components.Text

@Composable
fun AlertDialogWithIconExample() {
    var visible by remember { mutableStateOf(false) }
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
    ) {
        Button(onClick = { visible = true }) {
            Text(text = "Show dialog")
        }

        AlertDialog(
            visible = visible,
            onDismissRequest = { visible = false },
            icon = {
                Icon(
                    imageVector = Lucide.Bell,
                    contentDescription = null,
                    modifier = Modifier.size(24.dp),
                )
            },
            title = { Text("Enable notifications?", textAlign = TextAlign.Center) },
            text = {
                Text(
                    "Notifications help you keep up with important updates from this app.",
                    textAlign = TextAlign.Center,
                )
            },
            positiveButton = {
                Button(
                    onClick = { visible = false },
                    modifier = Modifier.fillMaxWidth(),
                    style = ButtonStyle.Primary,
                ) {
                    Text("Allow")
                }
            },
            negativeButton = {
                Button(
                    onClick = { visible = false },
                    modifier = Modifier.fillMaxWidth(),
                    style = ButtonStyle.Secondary,
                ) {
                    Text("Not now")
                }
            },
        )
    }
}

API Reference

AlertDialog

A modal dialog for confirmations and focused decisions.

@Composable
fun AlertDialog(
    visible: Boolean,
    onDismissRequest: () -> Unit,
    modifier: Modifier = Modifier,
    icon: (@Composable () -> Unit)? = null,
    title: (@Composable () -> Unit)? = null,
    text: @Composable () -> Unit,
    positiveButton: @Composable () -> Unit,
    neutralButton: (@Composable () -> Unit)? = null,
    negativeButton: (@Composable () -> Unit)? = null,
    shape: Shape = Theme[shapes][dialogShape],
    backgroundColor: Color = Theme[colors][panelColor],
    contentColor: Color = Theme[colors][onPanelColor],
    supportingTextColor: Color = Theme[colors][mutedColor],
    shadow: Shadow = Theme[shadows][overlayShadow],
)
Parameter Type Description
visible Boolean Whether the dialog is currently shown.
onDismissRequest () -> Unit Called when the dialog should close.
modifier Modifier Modifier applied to the dialog container.
icon (@Composable () -> Unit)? Optional icon content shown above the title.
title (@Composable () -> Unit)? Optional title content shown at the top of the dialog.
text @Composable () -> Unit Main supporting content shown inside the dialog.
positiveButton @Composable () -> Unit Primary action button content.
neutralButton (@Composable () -> Unit)? Optional neutral action button content.
negativeButton (@Composable () -> Unit)? Optional secondary or destructive action button content.
shape Shape Shape used for the dialog container.
backgroundColor Color Background color used for the dialog surface.
contentColor Color Color used for main dialog content.
supportingTextColor Color Color used for supporting text content.
shadow Shadow Shadow applied to the dialog container.

AlertDialog

Variant of [AlertDialog] that can take any content.

@Composable
fun AlertDialog(
    visible: Boolean,
    onDismissRequest: () -> Unit,
    modifier: Modifier = Modifier,
    paneTitle: String = "Alert dialog",
    shape: Shape = Theme[shapes][dialogShape],
    backgroundColor: Color = Theme[colors][panelColor],
    contentColor: Color = Theme[colors][onPanelColor],
    contentPadding: PaddingValues = PaddingValues(24.dp),
    shadow: Shadow = Theme[shadows][overlayShadow],
    content: @Composable () -> Unit,
)
Parameter Type Description
visible Boolean Whether the dialog is currently shown.
onDismissRequest () -> Unit Called when the dialog should close.
modifier Modifier Modifier applied to the dialog container.
paneTitle String Accessible title announced for the dialog panel.
shape Shape Shape used for the dialog container.
backgroundColor Color Background color used for the dialog surface.
contentColor Color Color used for the dialog content.
contentPadding PaddingValues Padding applied inside the dialog container.
shadow Shadow Shadow applied to the dialog container.
content @Composable () -> Unit Composable content displayed inside the dialog.