Open In App

How to Create a Custom Yes/No Dialog in Android with Kotlin?

Improve
Improve
Like Article
Like
Save
Share
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.



Last Updated : 30 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads