Published on Mar 02,2015
Email Post

While working on Androids you must have come across attractive alert boxes to get the users focus on something …for instance, ‘Do you want to Quit or Continue?’ or ‘Are you Done?’ So, what exactly is an AlertDialog in Android? AlertDialog is a class which is used to exhibit an alert in Androids.
Alert Dialog is an integral part of your Android applications which notifies the user of some urgent information that demands his/her immediate attention. In this tutorial, we will learn how to create an Alert Dialog in Android apps.

There are generally three regions in an AlertDialog:

1.  Title
‘Title’ is an optional region for you and should be used only when the content area is filled by a detailed message, a list or a custom layout. Thus, if you want to display a simple message or question, title is not required.

2.  Content Area
Content Area displays the message, a list or a custom layout for your alert.

3.  Action Buttons
Action Buttons are required to capture user’s actions such as ‘Cancel’, ‘Close’, ‘Yes’, ‘No’ and so on. There could be a maximum of three action buttons in an AlertDialog.

Here is a graphic below which displays all the three regions of an Alert Dialog:

Three regions of an Alert Dialog

Now once we have understood what an AlertDialog is all about, let’s explore how to create one!

While working with AlertDialog, we use some specific methods that are given below:

  • setTitle() method is used for setting the title.
  • setMessage() is used for setting the complete message.
  • setIcon() is used to set an icon if required.


  1. First, generally we use the AlertDialog.Builder class to develop an interface for the alert box, such as the title, message to display, buttons, and action buttons.
  2. Once this is done, we attach this builder to AlertDialog and call show() method to exhibit it. However, you can also attach some of the properties later in AlertDialog object, if you require.

There are three different Action Buttons you can add in an AlertDialog:

  • Positive button: Generally represents the ‘Yes’ button and should be used to accept and continue with the action.
  • Negative button: Generally represents the ‘No’ button and should be used to cancel the action.
  • Neutral button: These buttons perform between the positive and negative buttons. Neutral button is a button used to execute functionality like ‘Cancel’ shown in the graphic above. This button should be used when the user is not sure of continuing with or cancelling the action. For instance, the action could be “Remind me later.”

You can include only one of the each button types in an AlertDialog. This means you cannot have more than one ‘positive’ or one ‘negative’ or more than one ‘neutral’ button in a single Alert Dialog.

Here is the code snippet for displaying a simple AlertDialog with 1 action button:

AlertDialog.Builder builder = <b>new</b> AlertDialog.Builder(<b>this</b>);


.setPositiveButton("OK", <b>new</b> OnClickListener() {


<b>public</b> <b>void</b> onClick(DialogInterface dialog, <b>int</b> which) {

// <b>TODO</b> Auto-generated method stub





.setMessage("This is 1 Action alert Dialog, Press OK to continue");

AlertDialog diag = builder.create();;

Some AlertDialog Examples:

Examples of Alert Dialog

You can download the complete code from here!

[dl url=”#” class=’eModal eModal-27′ title=”Download Code” desc=”” type=”” align=”” for=”download”]

 Stay tuned for more tutorials in Android! Happy Learning!

Got a question for us? Mention them in the comments section and we will get back to you. 

Related Posts:

Understanding Android Emulators

Notification in Android

Android SDK Installation

Start your Android Development Course

About Author
shivani kapahi
Published on Mar 02,2015

Share on

Browse Categories

1 Comment