AlertDialog in Android using Jetpack Compose - GeeksforGeeks

25 Feb, 2021
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
Description
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.
Kotlin
import android.graphics.drawable.shapes.Shapeimport android.media.Imageimport android.os.Bundleimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivityimport androidx.compose.foundation.*import androidx.compose.foundation.Textimport androidx.compose.foundation.layout.*import androidx.compose.foundation.shape.CircleShapeimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.foundation.text.KeyboardOptionsimport androidx.compose.material.*import androidx.compose.material.Iconimport androidx.compose.material.icons.Iconsimport androidx.compose.material.icons.filled.AccountCircleimport androidx.compose.material.icons.filled.Infoimport androidx.compose.material.icons.filled.Menuimport androidx.compose.material.icons.filled.Phoneimport androidx.compose.runtime.*import androidx.compose.runtime.savedinstancestate.savedInstanceStateimport androidx.compose.ui.Alignmentimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.platform.setContentimport androidx.compose.ui.res.imageResourceimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport com.example.gfgapp.ui.GFGAppThemeimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clipimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.graphics.SolidColorimport androidx.compose.ui.platform.ContextAmbientimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.res.colorResourceimport androidx.compose.ui.text.TextStyleimport androidx.compose.ui.text.font.FontFamilyimport androidx.compose.ui.text.input.*import androidx.compose.ui.unit.Dpimport androidx.compose.ui.unit.TextUnit  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        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.                    AlertDialogComponent();                }            }        }    }}  @Preview(showBackground = true)@Composablefun DefaultPreview() {    GFGAppTheme {        AlertDialogComponent();    }}  @Composablefun 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.        AlertDialog(            // 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.                TextButton(                    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                TextButton(                    // 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: 
Android-Jetpack
Technical Scripter 2020
Android
Kotlin
Technical Scripter
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Retrofit with Kotlin Coroutine in Android
Flutter - Custom Bottom Navigation Bar
Android Listview in Java with Example
How to Read Data from SQLite Database in Android?
How to Change the Background Color After Clicking the Button in Android?
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
Android Menus
MVP (Model View Presenter) Architecture Pattern in Android with Example