Jetpack Compose – Alert Dialogs

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 applicati…


This content originally appeared on DEV Community and was authored by Bernat Borrás Paronella

Material Aalert Dialog

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 to false to remove it from our stack.

    title = {  },
    text = {  },

Add a Text to show a title and message of our AlertDialog

    confirmButton = { },
    dismissButton = { },

Define a Button or TextButton and its Text 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!

Alt Text


This content originally appeared on DEV Community and was authored by Bernat Borrás Paronella


Print Share Comment Cite Upload Translate Updates
APA

Bernat Borrás Paronella | Sciencx (2021-07-02T09:06:02+00:00) Jetpack Compose – Alert Dialogs. Retrieved from https://www.scien.cx/2021/07/02/jetpack-compose-alert-dialogs/

MLA
" » Jetpack Compose – Alert Dialogs." Bernat Borrás Paronella | Sciencx - Friday July 2, 2021, https://www.scien.cx/2021/07/02/jetpack-compose-alert-dialogs/
HARVARD
Bernat Borrás Paronella | Sciencx Friday July 2, 2021 » Jetpack Compose – Alert Dialogs., viewed ,<https://www.scien.cx/2021/07/02/jetpack-compose-alert-dialogs/>
VANCOUVER
Bernat Borrás Paronella | Sciencx - » Jetpack Compose – Alert Dialogs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/02/jetpack-compose-alert-dialogs/
CHICAGO
" » Jetpack Compose – Alert Dialogs." Bernat Borrás Paronella | Sciencx - Accessed . https://www.scien.cx/2021/07/02/jetpack-compose-alert-dialogs/
IEEE
" » Jetpack Compose – Alert Dialogs." Bernat Borrás Paronella | Sciencx [Online]. Available: https://www.scien.cx/2021/07/02/jetpack-compose-alert-dialogs/. [Accessed: ]
rf:citation
» Jetpack Compose – Alert Dialogs | Bernat Borrás Paronella | Sciencx | https://www.scien.cx/2021/07/02/jetpack-compose-alert-dialogs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.