Android Alert Dialog

Android Alert Dialog

Some times in your application , if you wanted to ask the user about taking a decision between yes or no in response of any particular action taken by the user, by remaining in the same activity and without changing the screen, you can use Alert Dialog.

In order to make an alert dialog , you need to make an object of AlertDialogBuilder which an inner class of AlertDialog. Its syntax is given below:

Now you have to set the positive (yes) , negative (no) or neutral button using the object of the AlertDialogBuilder class. Its syntax is:

Apart from this , you can use other functions provided by the builder class to customize the alert dialog. These are listed below:

setIcon(Drawable icon)This method set the icon of the alert dialog box.
setCancelable(boolean cancel able)This method sets the property that the dialog can be cancelled or not.
setMessage(CharSequence message)This method sets the message to be displayed in the alert dialog.
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)This method sets list of items to be displayed in the dialog as the content. The selected option will be notified by the listener.
setOnCancelListener(DialogInterface.OnCancelListener onCancelListener)This method Sets the callback that will be called if the dialog is cancelled.
setTitle(CharSequence title)This method set the title to be appear in the dialog.

After creating and setting the dialog builder , you will create an alert dialog by calling the create() method of the builder class. Its syntax is:

This will create the alert dialog and will show it on the screen.


In this example i will be demonstrating how to work on Android Alert Dialog.


Let’s get start by creating a project in Eclipse IDE.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as

2. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity.

3. Create a layout file for under res ⇒ layout folder. I named the layout file as activity_main.xml.

4. Now, add the following code in In this class we displayed different types of alert dialogs on button clicks.

5. Create a layout file for custom dialog under res ⇒ layout folder. I named the layout file as custom_dialog.xml.

6. Run the application and you will get the output as shown in video and you are done.

Thanks. :)

Download Source Code


About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)