Pop up a AlertDialog

An AlertDialog is the Android equivalent of an iPhone UIActionSheet or a view managed by a modal view controller. To create an AlertDialog, you first create a AlertDialog.Builder. The AlertDialog.Builder then builds the AlertDialog.

The AlertDialog is limited to three buttons: positive, negative, and neutral. If you want more buttons, see the dialog tutorial and ApiDemos/App/Dialog. The activity creates two types of OnClickListeners: View.OnClickListener and DialogInterface.OnClickListener. Why is the OnCancelListener set by the builder while the OnDismissListener is plugged into the finished dialog?

Three ways to make the dialog disappear

  1. Press one of the three dialog buttons. Calls onDismiss.
  2. Press the Android back button. Calls onCancel and onDismiss.
  3. Change app’s configuration (e.g., portrait to landcape orientation). Calls onDismiss and the dialog reappers in the new orientation.

Source code in AlertDialog.zip

  1. AlertDialogActivity.java
  2. R.java
  3. main.xml
  4. AndroidManifest.xml

AlertDialogs in ApiDemos/App/AlertDialogs

Source code is in AlertDialogSamples.java, alert_dialog.xml, and strings.xml.

  1. OK Cancel dialog with a message: case DIALOG_YES_NO_MESSAGE. Two buttons.
  2. OK Cancel dialog with a long message: case DIALOG_YES_NO_LONG_MESSAGE. Three buttons.
  3. OK Cancel dialog with ultra long message: case DIALOG_YES_NO_ULTRA_LONG_MESSAGE. You can scroll the message up and down.
  4. OK Cancel dialog with Holo Light theme: case DIALOG_YES_NO_HOLO_LIGHT_MESSAGE. Pass the theme AlertDialog.THEME_HOLO_LIGHT to the builder’s constructor.

Things to try

  1. Class R in the project’s gen/R.java file contains integer codes that stand for resources you created yourself (e.g., the views in main.xml). Class android.R contains integer codes for resources that are part of the operating system. For example, the code android.R.attr.alertDialogIcon istands for a generic icon that is part of the operating system; it is on your disk at android-sdks/platforms/android-14/data/res/drawable-hdpi/ic_dialog_alert.png; see alertDialogIcon in android-sdks/platforms/android-14/data/res/values/themes.xml.

    Give the dialog an icon of its own; see Dialog Icons. Download alert_dialog_icon.png (unfortunately identical to the icon we already have) and drag it into the project’s res/drawable-hdpi folder.
    Select how files should be imported into the project:
    • Copy files
    OK
    Replace the setIconAttribute with the following.

    		builder.setIcon(R.drawable.alert_dialog_icon);
    
  2. The generic title android.R.string.dialog_alert_title is part of the operating system. Give the dialog a title of its own. Change the setTitle to
    		builder.setTitle("The Meaning of Life");
    

  3. Chain the method calls together. After all, you don’t want your code to be understood by just anybody.
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
    		builder
    			.setTitle("The Meaning of Life")
    			.setMessage("Does life have a meaning?")
    			.setPositiveButton("Yes", onClickListener)
    			.setNeutralButton("Maybe", onClickListener)
    			.setNegativeButton("No", onClickListener)
    			//etc.
    

  4. The expression ++i == 1 is evaluated before the other expressions i == 2 and i == 3. The increment is therefore executed before any other use of the value of i. Simplify the code by breaking this statement to two separate statements.
    			++i;
    			String suffix = i == 1 ? "st" : i == 2 ? "nd" : i == 3 ? "rd" : "th";