AlertDialog
AlertDialogs provide important prompts in a user flow. They can require an action, communicate
AlertDialog
Composable Component
AlertDialogs provide important prompts in a user flow. They can require an action, communicate information, or help users accomplish a task. The AlertDialog is scrollable by default if the content exceeds the viewport height.
Android
@Composable
public fun AlertDialog(
visible: Boolean,
onDismissRequest: () -> Unit,
confirmButton: @Composable RowScope.() -> Unit,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
dismissButton: @Composable RowScope.() -> Unit = {
AlertDialogDefaults.DismissButton(onDismissRequest)
},
icon: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
verticalArrangement: Arrangement.Vertical = AlertDialogDefaults.VerticalArrangement,
contentPadding: PaddingValues =
if (icon != null) {
AlertDialogDefaults.confirmDismissWithIconContentPadding()
} else {
AlertDialogDefaults.confirmDismissContentPadding()
},
properties: DialogProperties = DialogProperties(),
content: (ScalingLazyListScope.() -> Unit)? = null,
)
Parameters
| visible | A boolean indicating whether the dialog should be displayed. |
| onDismissRequest | A lambda function to be called when the dialog is dismissed by swiping right (typically also called by the dismissButton). Implementation of this lambda must remove the dialog from the composition hierarchy e.g. by setting visible to false. |
| confirmButton | A slot for a Button indicating positive sentiment. Clicking the button must remove the dialog from the composition hierarchy e.g. by setting visible to false. It's recommended to use AlertDialogDefaults.ConfirmButton in this slot with onClick callback. |
| title | A slot for displaying the title of the dialog. Title should contain a summary of the dialog's purpose or content and should not exceed 3 lines of text. By default, TextOverflow.Ellipsis will be applied when text exceeds 3 lines. |
| modifier | Modifier to be applied to the dialog content. |
| dismissButton | A slot for a Button indicating negative sentiment. Clicking the button must remove the dialog from the composition hierarchy e.g. by setting visible to false. It's recommended to use AlertDialogDefaults.DismissButton in this slot with onClick callback. |
| icon | Optional slot for an icon to be shown at the top of the dialog. |
| text | Optional slot for displaying the message of the dialog below the title. Should contain additional text that presents further details about the dialog's purpose if the title is insufficient. |
| verticalArrangement | The vertical arrangement of the dialog's children. There is a default padding between icon, title, and text, which will be added to the spacing specified in this verticalArrangement parameter. |
| contentPadding | The padding to apply around the entire dialog's contents. It is recommended to use the defaults, which adjust to reduce the top padding when an icon is present. |
| properties | An optional DialogProperties object for configuring the dialog's behavior. |
| content | A slot for additional content, displayed within a scrollable ScalingLazyColumn. |
Android
@Composable
public fun AlertDialog(
visible: Boolean,
onDismissRequest: () -> Unit,
confirmButton: @Composable RowScope.() -> Unit,
title: @Composable () -> Unit,
transformationSpec: TransformationSpec,
modifier: Modifier = Modifier,
dismissButton: @Composable RowScope.() -> Unit = {
AlertDialogDefaults.DismissButton(onDismissRequest)
},
icon: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
verticalArrangement: Arrangement.Vertical = AlertDialogDefaults.VerticalArrangement,
contentPadding: PaddingValues =
if (icon != null) {
AlertDialogDefaults.confirmDismissWithIconContentPadding()
} else {
AlertDialogDefaults.confirmDismissContentPadding()
},
properties: DialogProperties = DialogProperties(),
content: (TransformingLazyColumnScope.() -> Unit)? = null,
)
Parameters
| visible | A boolean indicating whether the dialog should be displayed. |
| onDismissRequest | A lambda function to be called when the dialog is dismissed by swiping right (typically also called by the dismissButton). Implementation of this lambda must remove the dialog from the composition hierarchy e.g. by setting visible to false. |
| confirmButton | A slot for a Button indicating positive sentiment. Clicking the button must remove the dialog from the composition hierarchy e.g. by setting visible to false. It's recommended to use AlertDialogDefaults.ConfirmButton in this slot with onClick callback. |
| title | A slot for displaying the title of the dialog. Title should contain a summary of the dialog's purpose or content and should not exceed 3 lines of text. By default, TextOverflow.Ellipsis will be applied when text exceeds 3 lines. |
| transformationSpec | A spec that defines how items inside the TransformingLazyColumn transform and animate as they are scrolled. It is recommended to create a spec using rememberTransformationSpec. This parameter is the key differentiator from the standard AlertDialog overload, which uses a ScalingLazyColumn without per-item customizable animations. The provided transformationSpec is automatically applied only to the dialog's standard components (title, text, and buttons), while additional items in content lambda need to be handled manually, by applying a TransformationSpec to each of them. First item has a fixed spec, with a reduced transition area, so that the top item does not appear scaled or faded when the dialog is initially displayed. |
| modifier | Modifier to be applied to the dialog content. |
| dismissButton | A slot for a Button indicating negative sentiment. Clicking the button must remove the dialog from the composition hierarchy e.g. by setting visible to false. It's recommended to use AlertDialogDefaults.DismissButton in this slot with onClick callback. |
| icon | Optional slot for an icon to be shown at the top of the dialog. |
| text | Optional slot for displaying the message of the dialog below the title. Should contain additional text that presents further details about the dialog's purpose if the title is insufficient. |
| verticalArrangement | The vertical arrangement of the dialog's children. There is a default padding between icon, title, and text, which will be added to the spacing specified in this verticalArrangement parameter. |
| contentPadding | The padding to apply around the entire dialog's contents. It is recommended to use the defaults, which adjust to reduce the top padding when an icon is present. |
| properties | An optional DialogProperties object for configuring the dialog's behavior. |
| content | A slot for additional content, displayed within a scrollable TransformingLazyColumn. To ensure that your custom items animate consistently with the rest of the dialog, we recommend applying a TransformationSpec to every item. |
Android
@Composable
public fun AlertDialog(
visible: Boolean,
onDismissRequest: () -> Unit,
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
icon: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
verticalArrangement: Arrangement.Vertical = AlertDialogDefaults.VerticalArrangement,
contentPadding: PaddingValues =
if (icon != null) {
AlertDialogDefaults.contentWithIconPadding()
} else {
AlertDialogDefaults.contentPadding()
},
properties: DialogProperties = DialogProperties(),
content: (ScalingLazyListScope.() -> Unit)? = null,
)
Parameters
| visible | A boolean indicating whether the dialog should be displayed. |
| onDismissRequest | A lambda function to be called when the dialog is dismissed by swiping to the right or by other dismiss action. Implementation of this lambda must remove the dialog from the composition hierarchy e.g. by setting visible to false. |
| title | A slot for displaying the title of the dialog. Title should contain a summary of the dialog's purpose or content and should not exceed 3 lines of text. By default, TextOverflow.Ellipsis will be applied when text exceeds 3 lines. |
| modifier | Modifier to be applied to the dialog content. |
| icon | Optional slot for an icon to be shown at the top of the dialog. |
| text | Optional slot for displaying the message of the dialog below the title. Should contain additional text that presents further details about the dialog's purpose if the title is insufficient. |
| verticalArrangement | The vertical arrangement of the dialog's children. There is a default padding between icon, title, and text, which will be added to the spacing specified in this verticalArrangement parameter. |
| contentPadding | The padding to apply around the entire dialog's contents. It is recommended to use the defaults, which adjust to reduce the top padding when an icon is present. |
| properties | An optional DialogProperties object for configuring the dialog's behavior. |
| content | A slot for additional content, displayed within a scrollable ScalingLazyColumn. Any buttons added in this slot that are intended to dismiss the dialog must remove the dialog from the composition hierarchy e.g. by setting visible to false. |
Android
@Composable
public fun AlertDialog(
visible: Boolean,
onDismissRequest: () -> Unit,
title: @Composable () -> Unit,
transformationSpec: TransformationSpec,
modifier: Modifier = Modifier,
icon: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
verticalArrangement: Arrangement.Vertical = AlertDialogDefaults.VerticalArrangement,
contentPadding: @Composable (Boolean) -> PaddingValues = { isScrollable: Boolean ->
if (icon != null) {
AlertDialogDefaults.buttonStackWithIconContentPadding(isScrollable)
} else {
AlertDialogDefaults.buttonStackContentPadding(isScrollable)
}
},
properties: DialogProperties = DialogProperties(),
content: (TransformingLazyColumnScope.() -> Unit)? = null,
)
Parameters
| visible | A boolean indicating whether the dialog should be displayed. |
| onDismissRequest | A lambda function to be called when the dialog is dismissed by swiping to the right or by other dismiss action. Implementation of this lambda must remove the dialog from the composition hierarchy e.g. by setting visible to false. |
| title | A slot for displaying the title of the dialog. Title should contain a summary of the dialog's purpose or content and should not exceed 3 lines of text. By default, TextOverflow.Ellipsis will be applied when text exceeds 3 lines. |
| transformationSpec | A spec that defines how items inside the TransformingLazyColumn transform and animate as they are scrolled. It is recommended to create a spec using rememberTransformationSpec. This parameter is the key differentiator from the standard AlertDialog overload, which uses a ScalingLazyColumn without per-item customizable animations. The provided transformationSpec is automatically applied only to the dialog's standard components (title, text, and buttons), while additional items in content lambda need to be handled manually, by applying a TransformationSpec to each of them. First item has a fixed spec, with a reduced transition area, so that the top item does not appear scaled or faded when the dialog is initially displayed. |
| modifier | Modifier to be applied to the dialog content. |
| icon | Optional slot for an icon to be shown at the top of the dialog. |
| text | Optional slot for displaying the message of the dialog below the title. Should contain additional text that presents further details about the dialog's purpose if the title is insufficient. |
| verticalArrangement | The vertical arrangement of the dialog's children. There is a default padding between icon, title, and text, which will be added to the spacing specified in this verticalArrangement parameter. |
| contentPadding | The padding to apply around the entire dialog's contents. It is recommended to use the defaults, which adjust to reduce the top padding when an icon is present. |
| properties | An optional DialogProperties object for configuring the dialog's behavior. |
| content | A slot for additional content, displayed within a scrollable TransformingLazyColumn. To ensure that your custom items animate consistently with the rest of the dialog, we recommend applying a TransformationSpec to every item. |
Android
@Composable
public fun AlertDialog(
visible: Boolean,
onDismissRequest: () -> Unit,
edgeButton: (@Composable BoxScope.() -> Unit),
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
icon: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
verticalArrangement: Arrangement.Vertical = AlertDialogDefaults.VerticalArrangement,
contentPadding: PaddingValues =
if (icon != null) {
AlertDialogDefaults.contentWithIconPadding()
} else {
AlertDialogDefaults.contentPadding()
},
properties: DialogProperties = DialogProperties(),
content: (ScalingLazyListScope.() -> Unit)? = null,
)
Parameters
| visible | A boolean indicating whether the dialog should be displayed. |
| onDismissRequest | A lambda function to be called when the dialog is dismissed by swiping to the right or by other dismiss action. Implementation of this lambda must remove the dialog from the composition hierarchy e.g. by setting visible to false. |
| edgeButton | Slot for an EdgeButton indicating positive sentiment. Clicking the button must remove the dialog from the composition hierarchy e.g. by setting visible to false. It's recommended to use AlertDialogDefaults.EdgeButton in this slot with onClick callback. Note that when using an EdgeButton which is not Medium size, the contentPadding parameters should be specified. |
| title | A slot for displaying the title of the dialog. Title should contain a summary of the dialog's purpose or content and should not exceed 3 lines of text.By default, TextOverflow.Ellipsis will be applied when text exceeds 3 lines. |
| modifier | Modifier to be applied to the dialog content. |
| icon | Optional slot for an icon to be shown at the top of the dialog. |
| text | Optional slot for displaying the message of the dialog below the title. Should contain additional text that presents further details about the dialog's purpose if the title is insufficient. |
| verticalArrangement | The vertical arrangement of the dialog's children. There is a default padding between icon, title, and text, which will be added to the spacing specified in this verticalArrangement parameter. |
| contentPadding | The padding to apply around the entire dialog's contents. Bottom padding will be ignored and default spacing for the EdgeButton will be used. It is recommended to use the defaults, which adjust to reduce the top padding when an icon is present. |
| properties | An optional DialogProperties object for configuring the dialog's behavior. |
| content | A slot for additional content, displayed within a scrollable ScalingLazyColumn. Any buttons added in this slot that are intended to dismiss the dialog must remove the dialog from the composition hierarchy e.g. by setting visible to false. |
Android
@Composable
public fun AlertDialog(
visible: Boolean,
onDismissRequest: () -> Unit,
edgeButton: (@Composable BoxScope.() -> Unit),
title: @Composable () -> Unit,
transformationSpec: TransformationSpec,
modifier: Modifier = Modifier,
icon: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
verticalArrangement: Arrangement.Vertical = AlertDialogDefaults.VerticalArrangement,
contentPadding: PaddingValues =
if (icon != null) {
AlertDialogDefaults.contentWithIconPadding()
} else {
AlertDialogDefaults.contentPadding()
},
properties: DialogProperties = DialogProperties(),
content: (TransformingLazyColumnScope.() -> Unit)? = null,
)
Parameters
| visible | A boolean indicating whether the dialog should be displayed. |
| onDismissRequest | A lambda function to be called when the dialog is dismissed by swiping to the right or by other dismiss action. Implementation of this lambda must remove the dialog from the composition hierarchy e.g. by setting visible to false. |
| edgeButton | Slot for an EdgeButton indicating positive sentiment. Clicking the button must remove the dialog from the composition hierarchy e.g. by setting visible to false. It's recommended to use AlertDialogDefaults.EdgeButton in this slot with onClick callback. Note that when using an EdgeButton which is not Medium size, the contentPadding parameters should be specified. |
| title | A slot for displaying the title of the dialog. Title should contain a summary of the dialog's purpose or content and should not exceed 3 lines of text.By default, TextOverflow.Ellipsis will be applied when text exceeds 3 lines. |
| transformationSpec | A spec that defines how items inside the TransformingLazyColumn transform and animate as they are scrolled. It is recommended to create a spec using rememberTransformationSpec. This parameter is the key differentiator from the standard AlertDialog overload, which uses a ScalingLazyColumn without per-item customizable animations. The provided transformationSpec is automatically applied only to the dialog's standard components (title, text, and buttons), while additional items in content lambda need to be handled manually, by applying a TransformationSpec to each of them. First item has a fixed spec, with a reduced transition area, so that the top item does not appear scaled or faded when the dialog is initially displayed. |
| modifier | Modifier to be applied to the dialog content. |
| icon | Optional slot for an icon to be shown at the top of the dialog. |
| text | Optional slot for displaying the message of the dialog below the title. Should contain additional text that presents further details about the dialog's purpose if the title is insufficient. |
| verticalArrangement | The vertical arrangement of the dialog's children. There is a default padding between icon, title, and text, which will be added to the spacing specified in this verticalArrangement parameter. |
| contentPadding | The padding to apply around the entire dialog's contents. Bottom padding will be ignored and default spacing for the EdgeButton will be used. It is recommended to use the defaults, which adjust to reduce the top padding when an icon is present. |
| properties | An optional DialogProperties object for configuring the dialog's behavior. |
| content | A slot for additional content, displayed within a scrollable TransformingLazyColumn. Any buttons added in this slot that are intended to dismiss the dialog must remove the dialog from the composition hierarchy e.g. by setting visible to false. To ensure that your custom items animate consistently with the rest of the dialog, we recommend applying a TransformationSpec to each of them - see samples. |
Code Examples
AlertDialogWithConfirmAndDismissSample
@Composable
@Preview
fun AlertDialogWithConfirmAndDismissSample() {
var showDialog by remember { mutableStateOf(false) }
Box(Modifier.fillMaxSize()) {
FilledTonalButton(
modifier = Modifier.align(Alignment.Center),
onClick = { showDialog = true },
label = { Text("Show Dialog") },
)
}
AlertDialog(
visible = showDialog,
onDismissRequest = { showDialog = false },
icon = {
Icon(
Icons.Rounded.AccountCircle,
modifier = Modifier.size(32.dp),
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
)
},
title = { Text("Enable Battery Saver Mode?") },
text = { Text("Your battery is low. Turn on battery saver.") },
confirmButton = {
AlertDialogDefaults.ConfirmButton(
onClick = {
// Perform confirm action here
showDialog = false
}
)
},
dismissButton = {
AlertDialogDefaults.DismissButton(
onClick = {
// Perform dismiss action here
showDialog = false
}
)
},
) {
item { Text(text = "You can configure battery saver mode in the setting menu.") }
item { Button(onClick = {}) { Text(text = "Go to settings") } }
}
}
AlertDialogWithConfirmAndDismissTransformingContentSample
@Preview
@Composable
fun AlertDialogWithConfirmAndDismissTransformingContentSample() {
var showDialog by remember { mutableStateOf(false) }
val transformationSpec = rememberTransformationSpec()
Box(Modifier.fillMaxSize()) {
FilledTonalButton(
modifier = Modifier.align(Alignment.Center),
onClick = { showDialog = true },
label = { Text("Show Dialog") },
)
}
AlertDialog(
visible = showDialog,
onDismissRequest = { showDialog = false },
icon = {
Icon(
Icons.Rounded.AccountCircle,
modifier = Modifier.size(32.dp),
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
)
},
title = { Text("Enable Battery Saver Mode?") },
text = { Text("Your battery is low. Turn on battery saver.") },
transformationSpec = transformationSpec,
confirmButton = {
AlertDialogDefaults.ConfirmButton(
onClick = {
// Perform confirm action here
showDialog = false
}
)
},
dismissButton = {
AlertDialogDefaults.DismissButton(
onClick = {
// Perform dismiss action here
showDialog = false
}
)
},
) {
item {
Text(
modifier =
Modifier.transformedHeight(this, transformationSpec).graphicsLayer {
with(SurfaceTransformation(transformationSpec)) {
applyContentTransformation()
applyContainerTransformation()
}
},
text = "You can configure battery saver mode in the setting menu.",
)
}
item {
Button(
modifier = Modifier.transformedHeight(this, transformationSpec),
transformation = SurfaceTransformation(transformationSpec),
onClick = {},
) {
Text(text = "Go to settings")
}
}
}
}
AlertDialogWithContentGroupsSample
@Preview
@Composable
fun AlertDialogWithContentGroupsSample() {
var showDialog by remember { mutableStateOf(false) }
var weatherEnabled by remember { mutableStateOf(false) }
var calendarEnabled by remember { mutableStateOf(false) }
Box(Modifier.fillMaxSize()) {
FilledTonalButton(
modifier = Modifier.align(Alignment.Center),
onClick = { showDialog = true },
label = { Text("Show Dialog") },
)
}
AlertDialog(
visible = showDialog,
onDismissRequest = { showDialog = false },
title = { Text("Share your location") },
text = { Text(" The following apps have asked you to share your location") },
edgeButton = {
AlertDialogDefaults.EdgeButton(
onClick = {
// Perform confirm action here
showDialog = false
}
) {
Text("Share once")
}
},
) {
item {
SwitchButton(
modifier = Modifier.fillMaxWidth(),
checked = weatherEnabled,
onCheckedChange = { weatherEnabled = it },
label = { Text("Weather") },
)
}
item {
SwitchButton(
modifier = Modifier.fillMaxWidth(),
checked = calendarEnabled,
onCheckedChange = { calendarEnabled = it },
label = { Text("Calendar") },
)
}
item { AlertDialogDefaults.GroupSeparator() }
item {
FilledTonalButton(
modifier = Modifier.fillMaxWidth(),
onClick = {},
label = { Text(modifier = Modifier.fillMaxWidth(), text = "Never share") },
)
}
item {
FilledTonalButton(
modifier = Modifier.fillMaxWidth(),
onClick = {},
label = { Text(modifier = Modifier.fillMaxWidth(), text = "Share always") },
)
}
}
}
AlertDialogWithContentGroupsTransformingContentSample
@Preview
@Composable
fun AlertDialogWithContentGroupsTransformingContentSample() {
var showDialog by remember { mutableStateOf(false) }
var weatherEnabled by remember { mutableStateOf(false) }
var calendarEnabled by remember { mutableStateOf(false) }
val transformationSpec = rememberTransformationSpec()
Box(Modifier.fillMaxSize()) {
FilledTonalButton(
modifier = Modifier.align(Alignment.Center),
onClick = { showDialog = true },
label = { Text("Show Dialog") },
)
}
AlertDialog(
visible = showDialog,
onDismissRequest = { showDialog = false },
title = { Text("Share your location") },
text = { Text(" The following apps have asked you to share your location") },
transformationSpec = transformationSpec,
edgeButton = {
AlertDialogDefaults.EdgeButton(
onClick = {
// Perform confirm action here
showDialog = false
}
) {
Text("Share once")
}
},
) {
item {
SwitchButton(
modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec),
checked = weatherEnabled,
onCheckedChange = { weatherEnabled = it },
label = { Text("Weather") },
transformation = SurfaceTransformation(transformationSpec),
)
}
item {
SwitchButton(
modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec),
checked = calendarEnabled,
onCheckedChange = { calendarEnabled = it },
label = { Text("Calendar") },
transformation = SurfaceTransformation(transformationSpec),
)
}
item { AlertDialogDefaults.GroupSeparator() }
item {
FilledTonalButton(
modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec),
onClick = {},
label = { Text(modifier = Modifier.fillMaxWidth(), text = "Never share") },
transformation = SurfaceTransformation(transformationSpec),
)
}
item {
FilledTonalButton(
modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec),
onClick = {},
label = { Text(modifier = Modifier.fillMaxWidth(), text = "Share always") },
transformation = SurfaceTransformation(transformationSpec),
)
}
}
}
AlertDialogWithEdgeButtonSample
@Preview
@Composable
fun AlertDialogWithEdgeButtonSample() {
var showDialog by remember { mutableStateOf(false) }
Box(Modifier.fillMaxSize()) {
FilledTonalButton(
modifier = Modifier.align(Alignment.Center),
onClick = { showDialog = true },
label = { Text("Show Dialog") },
)
}
AlertDialog(
visible = showDialog,
onDismissRequest = { showDialog = false },
icon = {
Icon(
Icons.Rounded.AccountCircle,
modifier = Modifier.size(32.dp),
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
)
},
title = { Text("Mobile network is not currently available") },
text = { Text("Please try again later or check your network settings.") },
edgeButton = {
AlertDialogDefaults.EdgeButton(
onClick = {
// Perform confirm action here
showDialog = false
}
)
},
)
}
AlertDialogWithEdgeButtonTransformingContentSample
@Preview
@Composable
fun AlertDialogWithEdgeButtonTransformingContentSample() {
var showDialog by remember { mutableStateOf(false) }
Box(Modifier.fillMaxSize()) {
FilledTonalButton(
modifier = Modifier.align(Alignment.Center),
onClick = { showDialog = true },
label = { Text("Show Dialog") },
)
}
AlertDialog(
visible = showDialog,
onDismissRequest = { showDialog = false },
icon = {
Icon(
Icons.Rounded.AccountCircle,
modifier = Modifier.size(32.dp),
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
)
},
title = { Text("Mobile network is not currently available") },
text = { Text("Please try again later or check your network settings.") },
transformationSpec = rememberTransformationSpec(),
edgeButton = {
AlertDialogDefaults.EdgeButton(
onClick = {
// Perform confirm action here
showDialog = false
}
)
},
)
}
Create your own Component Library
Material Components are meant to be used as is and they do not allow customizations. To build your own Jetpack Compose component library use Compose Unstyled