How to Create a Custom Yes/No Dialog in Android with Kotlin? Last Updated : 30 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Android AlertDialog is used to display a small window to the user to make a decision via an OK, Yes, or Cancel button or enter additional information. It is normally modal and requires the user to take any action before they can proceed. In this article, we will use AlertDialog to create a reusable generic solution when the user simply needs to reply Yes or No. A sample video is given below to get an idea about what we are going to do in this article. Getting Started Open Android Studio and import the starter project. Create a new class CustomDialog.kt to contain our dialog. Our CustomDialog is initialized with a context as it is needed by AlertDialog.Builder. The show function sets up the title and the content of the message to be displayed to the user. Kotlin import android.app.AlertDialog import android.content.Context class CustomDialog(context: Context) : AlertDialog.Builder(context) { fun show(title: String, message: String) { val builder = AlertDialog.Builder(context) builder.setTitle(title) builder.setMessage(message) builder.setIcon(android.R.drawable.ic_dialog_alert) // Create the AlertDialog val alertDialog: AlertDialog = builder.create() // Set other dialog properties alertDialog.setCancelable(false) alertDialog.show() } } Now we need to implement a mechanism to let the user react depending on which button is clicked. For that, we will use the possibility in Kotlin to declare a function as a variable. Here onResponse is a function that takes 1 argument of type ResponseType and returns nothing. The enumerate ResponseType list the possible buttons in our dialog. We could also add CANCEL. Kotlin lateinit var onResponse: (r : ResponseType) -> Unit enum class ResponseType { YES, NO } Now the show function needs to receive the user ResponseType function as a parameter and call it when one of the 2 buttons is clicked. The final code for CustomDialog Kotlin import android.app.AlertDialog import android.content.Context class WineDialog(context: Context) : AlertDialog.Builder(context) { lateinit var onResponse: (r : ResponseType) -> Unit enum class ResponseType { YES, NO, CANCEL } fun show(title: String, message: String, listener: (r : ResponseType) -> Unit) { val builder = AlertDialog.Builder(context) builder.setTitle(title) builder.setMessage(message) builder.setIcon(android.R.drawable.ic_dialog_alert) onResponse = listener // performing positive action builder.setPositiveButton("Yes") { _, _ -> onResponse(ResponseType.YES) } // performing negative action builder.setNegativeButton("No") { _, _ -> onResponse(ResponseType.NO) } // Create the AlertDialog val alertDialog: AlertDialog = builder.create() // Set other dialog properties alertDialog.setCancelable(false) alertDialog.show() } } Indeed we could also call onResponse for the CANCEL button. Now somewhere in your MainActivity for example, you could write something like: Kotlin CustomDialog(context).show(getString(R.string.alert_title), getString(R.string.are_you_sure)) { Toast.makeText(applicationContext, it.toString(), Toast.LENGTH_LONG).show() } Output: Indeed you can customize more using an XML layout. It is also possible to provide a TextView or even a Spinner in the dialog. Comment More infoAdvertise with us Next Article How to Create a Custom Yes/No Dialog in Android with Kotlin? B bertrandpivaty Follow Improve Article Tags : Kotlin Android Similar Reads How to Create Dialog with Custom Layout in Android? In Android, A dialog is a small window that prompts the user to make a decision, provide some additional information, and inform the user about some particular task. The following are the main purposes or goals of a dialog To warn the user about any activity.To inform the user about any activity.To 3 min read How to Create Option Menu in Android using Kotlin? In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app. 2 min read How to Make a Custom Exit Dialog in Android? In this tutorial, we are going to create a Custom Exit Dialog in Android. By default, android doesn't provide any exit dialog, but we can create it using the dialog class in java. But most of the developers and also the user don't like the default dialog box and also we can't do any modification in 5 min read How to Create Google Sign In UI using Android Studio? Nowadays, android apps are very popular. This UI has generally seen in the âGoogle Sign Inâ App. In this article, we will create a Google Sign UI in Android. Below are the various steps on how to do it. This will help the beginner to build some awesome UI in android by referring to this article. St 3 min read How to Implement Custom Dialog Maker in Android? In this article, we are going to make an application of Custom Dialog Maker in android studio. In this application, we can create dialogs of our own choice of style, type, and animation. What is a dialog? A dialog is a small window that prompts the user to make a decision, provide some additional in 5 min read How to add a custom styled Toast in Android using Kotlin A Toast is a short alert message shown on the Android screen for a short interval of time. Android Toast is a short popup notification that is used to display information when we perform any operation in the app. In this article, let's learn how to create a custom toast in Android using Kotlin.To cr 4 min read How to Draw Border on Event of Click in Android with Kotlin? Sometimes we want to display to the users that they have clicked an item by showing a border on the item. So, for this, we need to create two drawable resource files, one for the normal background of the item and another for the border. A sample video is given below to get an idea about what we are 3 min read How to Display a Yes/No DialogBox in Android? DialogBox is used in many android applications to display dialogs. There are different types of dialogs that are displayed within android applications such as AlertDialog, warning dialogs, and others. In this article, we will take a look at How to Display a Yes/No Dialog Box in Android. A sample vid 4 min read How to Create an Alert Dialog Box in Android? An Android Alert Dialog is a UI element that displays a warning or notification message and asks the user to respond with options such as Yes or No. Based on the user's response, appropriate actions are executed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Ac 4 min read How to Change Alert Dialog Position on Screen in Android? AlertDialog in Android is an alert message that appears in the form of a pop-up consisting of four elements namely a title, a message, a positive button, and a negative button. The positive and the negative buttons are clickable and can be programmed for performing an action. However, the AlertDialo 2 min read Like