Android Alert Dialog Tutorial with Example-How to create alert dialog in android

By | May 26, 2019

What is Alert Dialog ?

A Dialog is small window that prompts the user to a decision or enter additional information.

Alert Dialog use to some time application to ask user taking decision between yes or no in response of any particular action taken by the user.And it is open same activity does not change another screen.

Another use to display the dialog message to OK and cancel button.It can be used to ask user to choice option and continue or not.

Alert Dialog is a 3 composed of regions :Title,content area and action Buttons.It is subclass of Dialog class.

Implementation of Methods

setTitle( ) : This method is use to set the title of AlertDialog

setMessage() :  This method for displaying the message.

setIcon() : method is use to set the icon on Alert dialog box.

Then Add Two Button is setPositiveButton and setNegativeButton  to show alert dialog display.

How to Create Alert Dialog ?

Now i have create activity_main.xml file following this code.

Now create File and follow this code :

Now Above this code i have implement of one button in xml file and find id of button in java class. when button is click to create object of AlertDialog.Builder and then this object to call all methods like set title,setMessage and positive,negative button.and in this button click to display toast message and last code is display dialog to use show() method to display the dialog.