Open In App

AlertDialog in Android using Jetpack Compose

Last Updated : 25 Feb, 2021
Like Article

AlertDialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response, the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.

We have seen AlertDialog in many of the apps, this is used to display a kind of message to our users. In this article, we will see the implementation of Alert Dialog in Android using Jetpack Compose

Attributes of AlertDialog



onDismissRequest to check if the alert dialog is open or not. 
title to add title in our alert dialog. 
text to add a text description to our Alert Dialog. 
confirmButton to set a button for confirmation in Alert Dialog. 
dismissButton to set a dismiss dialog button in Alert Dialog. 
backgroundColor to set a background color of an alert dialog. 
contentColor to set a color for our content of Alert Dialog. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in the Android Studio Canary Version please refer to How to Create a new Project in Android Studio Canary Version with Jetpack Compose.

Step 2: Working with the MainActivity.kt file

Navigate to the app > java > your app’s package name and open the MainActivity.kt file. Inside that file add the below code to it. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle
import android.widget.Toast
import androidx.compose.material.*
import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Phone
import androidx.compose.runtime.*
import androidx.compose.runtime.savedinstancestate.savedInstanceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.gfgapp.ui.GFGAppTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            ScrollableColumn {
                // A surface container using the 
                  // 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    // at below line we are calling our 
                      // function for alert dialog.
@Preview(showBackground = true)
fun DefaultPreview() {
    GFGAppTheme {
fun AlertDialogComponent() {
    // below line is use to get
    // the context for our app.
    val context = ContextAmbient.current
    // below line is use to set our state
    // of dialog box to open as true.
    val openDialog = remember { mutableStateOf(true) }
    // below line is to check if the
    // dialog box is open or not.
    if (openDialog.value) {
        // below line is use to
        // display a alert dialog.
            // on dialog dismiss we are setting
            // our dialog value to false.
            onDismissRequest = { openDialog.value = false },
            // below line is use to display title of our dialog
            // box and we are setting text color to white.
            title = { Text(text = "Geeks for Geeks", color = Color.White) },
            // below line is use to display
            // description to our alert dialog.
            text = { Text("Hello! This is our Alert Dialog..", color = Color.White) },
            // in below line we are displaying
            // our confirm button.
            confirmButton = {
                // below line we are adding on click 
                // listener for our confirm button.
                    onClick = {
                        openDialog.value = false
                        Toast.makeText(context, "Confirm Button Click", Toast.LENGTH_LONG).show()
                ) {
                    // in this line we are adding 
                    // text for our confirm button.
                    Text("Confirm", color = Color.White)
            // in below line we are displaying
            // our dismiss button.
            dismissButton = {
                // in below line we are displaying
                // our text button
                    // adding on click listener for this button
                    onClick = {
                        openDialog.value = false
                        Toast.makeText(context, "Dismiss Button Click", Toast.LENGTH_LONG).show()
                ) {
                    // adding text to our button.
                    Text("Dismiss", color = Color.White)
            // below line is use to add background color to our alert dialog
            backgroundColor = colorResource(id = R.color.teal_200),
            // below line is use to add content color for our alert dialog.
            contentColor = Color.White

Now run your app and see the output of the app: 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads