Android AlertDialog With Examples

Android AlertDialog is used in cases when we require the user to choose an option from the prompt presented on the screen. In this tutorial we will be discussing about creating different Alert Dialog with one button (OK button), two buttons (Yes and No) and three buttons (Yes, No and Cancel).

Android AlertDialog

Below is the final android alert dialog application we will create in this tutorial.

android alertdialog example

Android AlertDialog is one of the most important and basic component in Android applications. Alert dialog box is used to show alerts to the users, get confirmation from the users. In order to make an alert dialog, we need to make an object of AlertDialogBuilder which is an inner class of AlertDialog. Its syntax is given below.

Android AlertDialog Components

  1. Title: Note that title is optional
  2. Content: This displays the message to the user. It can be a string message, a list or custom layout
  3. Action Buttons: These buttons are of three types. They are Positive, Negative and Neutral action buttons. An alert dialog can have maximum three action buttons. In general following are the conventions for the buttons:
    • If you want the user to accept the action, use Positive action button. It is normally displayed as OK/YES.
    • If the user wants to cancel the action, then you can use Negative action button (NO).
    • If the user wants to postpone the decision use Neutral action button (Later).

Now we need to set the methods of these buttons using AlertDialogBuilder class. The syntax is given below.

The first argument is the text to be displayed. The second argument is the listener to be invoked when the positive button is pressed.

AlertDialog Functions

Besides this, there are some functions available to customise the alert dialog as given below.

  1. setIcon(Drawable icon): This method set the icon of the alert dialog box
  2. setCancelable(boolean cancelable): This method sets the property that the dialog can be cancelled or not
  3. setMessage(CharSequence message): This method sets the message to be displayed in the alert dialog
  4. 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
  5. setOnCancelListener(DialogInterface.OnCancelListener onCancelListener): This method Sets the callback that will be called if the dialog is cancelled
  6. setTitle(CharSequence title): This method set the title to be appear in the dialog
  7. getListView(): This function is used to get the list view used in the dialog

After creating and setting the dialog builder, we need to create an alert dialog by calling the create() method of the builder class. Its syntax is shown below.

Android AlertDialog Example Project Structure

android-alert-dialog-project-234x450

This application consists of three buttons which opens alert dialog containing the respective number of buttons.

Android Alert Dialog Project Code

The application consists of a single activity. The layout of the activity has three buttons as shown in the xml file below.

activity_alert.xml

The AlertActivity.java is given below. Each button click invokes an alert dialog with different set of action buttons. On clicking action buttons a Toast corresponding to that action button is displayed.

This brings an end to this tutorial. We’ll discuss how to customize alert dialog at length later. You can download the final android alert dialog project from the below link.

Reference: Official Doc

By admin

Leave a Reply