

Jetpack Compose - Alert Dialogs
source link: https://dev.to/adevintaspain/jetpack-compose-alert-dialogs-8j
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The code presented in this article is using compose rc01
Jetpack Compose brings us with a cool new UI toolkit, but some things are completely different from how we are used to it, one of these cases is AlertDialog
a common component on any application.
Steps to display an AlertDialog on compose:
- Create a state as a flag to check if we need to show the dialog or not.
var showDialog by remember { mutableStateOf(false) }
You may need to import:
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
Basic AlertDialog:
if (showDialog) {
AlertDialog(
onDismissRequest = { },
title = { },
text = { },
confirmButton = { },
dismissButton = { },
)
}
Let's see what's each param of AlertDialog
composable method:
onDismissRequest = { },
Will be called when clicking outside of a dialog, here we must set
showDialog
tofalse
to remove it from our stack.
title = { },
text = { },
Add a
Text
to show a title and message of ourAlertDialog
confirmButton = { },
dismissButton = { },
Define a
Button
orTextButton
and itsText
to get actions like "Accept" or "Cancel"
When click on confirm or button we must set our showDialog
to false also, to avoid showing the dialog on the next recomposition.
onDismissRequest = { showDialog = false },
confirmButton = { showDialog = false },
dismissButton = { showDialog = false },
Other dialogs
What if we want to display custom content, like a list of items to select one?
text = {
LazyColumn {
items(...) { item -> Text(text = item) }
}
},
Use the text
field of AlertDialog
to display any content that is needed and handle the dialog in the same way of buttons: showDialog = false
.
Hope you enjoyed the article!
Recommend
-
47
Sample Dialogs: The Key to Creating Great Actions on Google 2018-09-07adminGoogleDevFeeds
-
38
One of the most empowering things you can learn as a new front-end developer who is starting to learn JavaScript isto change classes. If you can change classes, you can use your CSS skills to control a lot on a...
-
73
Bootstrap 4 dialogs Doc and demos here : https://www.bgaze.fr/bootstrap4-dialogs Table of content : Quick start BSD re...
-
4
For the love of all that's holy, please progressively enhance modal "download our app" dialogsFor the love of all that's holy, please progressively enhance modal "download our app" dialogs 03 Dec 2013
-
15
Closed Bug 1674806 Opened 2 months ago Closed 10 days ago...
-
11
Edit page Dialogs...
-
8
Manage ATK key events on transient dialogs. (I47c98185) · Gerrit Code Review
-
3
Visual Feedback: Dialogs, Snackbars & Toasts Work with Touch States...
-
5
React Native Paper Alerts Cross Platform Material Alert and Prompt for React Native. It tries to follow the API and function signature of React Native's built-in Alert Module and works on Android, IOS and Web. Color Scheme Simple...
-
20
Compose Material Dialogs Easy to use library to help you build complex dialogs using Jetpack Compose
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK