Short answer: in Jetpack Compose you show an AlertDialog by calling it conditionally from a boolean state. Pass onDismissRequest, a confirmButton and, optionally, a title, text and dismissButton. When the state becomes false, the dialog is removed.
Basic confirm dialog
@Composable
fun ConfirmDeleteDialog(
onConfirm: () -> Unit,
onDismiss: () -> Unit
) {
AlertDialog(
onDismissRequest = onDismiss,
title = { Text("Delete item?") },
text = { Text("This action cannot be undone.") },
confirmButton = {
TextButton(onClick = onConfirm) { Text("Delete") }
},
dismissButton = {
TextButton(onClick = onDismiss) { Text("Cancel") }
}
)
}
Show and hide the dialog with state
@Composable
fun DialogDemo() {
var showDialog by remember { mutableStateOf(false) }
Button(onClick = { showDialog = true }) {
Text("Delete")
}
if (showDialog) {
ConfirmDeleteDialog(
onConfirm = {
// delete the item here
showDialog = false
},
onDismiss = { showDialog = false }
)
}
}
Compose dialogs are not “shown” by a function call. They exist while they are part of the composition. Keep a boolean (or a nullable item) in state, and call the dialog only when it should be visible. Add import androidx.compose.runtime.getValue and setValue if by remember shows an error.
Add an icon
AlertDialog(
onDismissRequest = onDismiss,
icon = { Icon(Icons.Default.Warning, contentDescription = null) },
title = { Text("Delete item?") },
text = { Text("This action cannot be undone.") },
confirmButton = { TextButton(onClick = onConfirm) { Text("Delete") } },
dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } }
)
Dialog with a text field
@Composable
fun RenameDialog(
currentName: String,
onSave: (String) -> Unit,
onDismiss: () -> Unit
) {
var name by remember { mutableStateOf(currentName) }
AlertDialog(
onDismissRequest = onDismiss,
title = { Text("Rename") },
text = {
OutlinedTextField(
value = name,
onValueChange = { name = it },
singleLine = true,
label = { Text("Name") }
)
},
confirmButton = {
TextButton(
onClick = { onSave(name) },
enabled = name.isNotBlank()
) { Text("Save") }
},
dismissButton = {
TextButton(onClick = onDismiss) { Text("Cancel") }
}
)
}
Fully custom dialog
When you need your own layout, use the lower-level Dialog with a Card or Surface:
@Composable
fun CustomDialog(onDismiss: () -> Unit) {
Dialog(onDismissRequest = onDismiss) {
Card(
shape = RoundedCornerShape(16.dp),
modifier = Modifier.fillMaxWidth()
) {
Column(
modifier = Modifier.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Text("Custom dialog", style = MaterialTheme.typography.titleLarge)
Text("Put any composables here.")
Button(onClick = onDismiss) { Text("Close") }
}
}
}
}
Import androidx.compose.ui.window.Dialog. To control dismissal, pass properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false).
Common mistakes
- Not updating state in
onDismissRequest. If you leave it empty, the dialog never closes when the user taps outside or presses back. - Keeping dialog logic inside the dialog. Hoist state to the caller, as in the examples, so it is easy to test and reuse.
- Creating the dialog outside an
if. It will show every time that code runs. - Using the same state for several dialogs. Use one state value per dialog, or a sealed type such as
DialogState.
FAQ
How do I show an AlertDialog in Jetpack Compose?
Keep a boolean state and call AlertDialog inside an if (showDialog) block.
How do I close an AlertDialog?
Set the state to false in onDismissRequest and in the button click handlers.
What is the difference between AlertDialog and Dialog?
AlertDialog gives you Material 3 slots for title, text, icon and buttons. Dialog is a bare window where you supply the whole layout.
Can an AlertDialog contain a TextField?
Yes. Put the text field in the text slot and keep its value in state, as shown above.
More: Jetpack Compose tutorials and navigation drawer in Compose.