August 27, 2020

Different types of AlertDialog in Android

By Mohit Agrawal

Learn the basic way of implementing different types of AlertDialog in Android

Introduction

alert dialog android 1

Let’s learn one of the basic concepts of android that is AlertDialog. It is useful when you want to inform the user about some events and giving them a chance to respond to it.

In today’s android tutorial I will show you the implementation of AlertDialog with one, two, and three buttons. At the end of this tutorial, you will able to decide which one best suits your need.

Now, you may have the question that even Toast Notification is used to inform the user about some event then what is the difference between Toast and AlertDialog. Let’s see it.

AlertDialog VS Toast

Toast is a small pop up that is generally used to provide some general feedback about an event. Toasts automatically disappear after a timeout. It is not user interactive.

But Alert Dialog is doesn’t disappear automatically. It waits for user interaction. Based on the option selected by the user we can execute the expected logic.

Implementation

Alert Dialog with one button

Here I am showing the alert dialog on the click of the listener of a button as shown the below code block.

import android.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity


button1.setOnClickListener {
            val builder = AlertDialog.Builder(this)
            //set title for alert dialog
            builder.setTitle("Alert")
            //set message for alert dialog
            builder.setMessage("You do not have access to this file.")
            //builder.setIcon(android.R.drawable.ic_dialog_alert)

            //performing positive action
            builder.setPositiveButton("Okay"){dialogInterface, which ->
                Toast.makeText(applicationContext,"clicked okay",Toast.LENGTH_LONG).show()
            }

            // Create the AlertDialog
            val alertDialog: AlertDialog = builder.create()
            // Set other dialog properties
            alertDialog.setCancelable(false)
            alertDialog.show()
        }

Now let’s understand the code. Most of the things are pretty self-explanatory here but there are few things which I like to focus on.

First I am creating an object of a AlertDialog.Builder(this)then setting the title and message for the alert dialog. But the thing you must understand is the builder.setPositiveButton().

Inside setPositiveButton()we pass the button text along with the code which we want to trigger when users click on the button.

At the end use alertDialog.show() to present the dialog to the user.

Alert Dialog with two button

alert dialog android 3
val builder = AlertDialog.Builder(this)
            //set title for alert dialog
            builder.setTitle("Alert")
            //set message for alert dialog
            builder.setMessage("Do you want to continue?")
            //builder.setIcon(android.R.drawable.ic_dialog_alert)

            //performing positive action
            builder.setPositiveButton("Yes"){dialogInterface, which ->
                Toast.makeText(applicationContext,"clicked yes",Toast.LENGTH_LONG).show()
            }

            //performing negative action
            builder.setNegativeButton("No"){dialogInterface, which ->
                Toast.makeText(applicationContext,"clicked No", Toast.LENGTH_LONG).show()
            }
            // Create the AlertDialog
            val alertDialog: AlertDialog = builder.create()
            // Set other dialog properties
            alertDialog.setCancelable(false)
            alertDialog.show()

It is same as the first one only difference is that we are using one more type of alert dialog button which is setNegativeButton(). Here also we pass the text for the button and the code block which we want to execute when the user clicks it.

Alert Dialog with two button

alert dialog android 2

This is the last one where the third button is the setNeutralButton(). Here is the code for the same.

val builder = AlertDialog.Builder(this)
            //set title for alert dialog
            builder.setTitle("Alert")
            //set message for alert dialog
            builder.setMessage("Your membership will expire in next 3 days.")
            //builder.setIcon(android.R.drawable.ic_dialog_alert)

            //performing positive action
            builder.setPositiveButton("Renew"){dialogInterface, which ->
                Toast.makeText(applicationContext,"clicked renew",Toast.LENGTH_LONG).show()
            }
            //performing cancel action
            builder.setNeutralButton("Remind me later"){dialogInterface , which ->
                Toast.makeText(applicationContext,"clicked Remind me later",Toast.LENGTH_LONG).show()
            }
            //performing negative action
            builder.setNegativeButton("Cancel"){dialogInterface, which ->
                Toast.makeText(applicationContext,"clicked cancel", Toast.LENGTH_LONG).show()
            }
            // Create the AlertDialog
            val alertDialog: AlertDialog = builder.create()
            // Set other dialog properties
            alertDialog.setCancelable(false)
            alertDialog.show()

Conclusion

Now you can choose any of the AlertDialog which suits the best for your project and implement it. If you are facing any issue while doing the implementation then please let me know it in the comment section.

Check out: Custom Alert Dialog in Android

Subscribe YouTube: More tutorials like this

I hope this blog post is useful for you, do let me know your opinion in the comment section below.
I will be happy to see your comments down below 👏.
Thanks for reading!!!