Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Creating a Pop-up/Alert Dialog

Author: Rachee Singh -- Published? true -- FormatLanguage: W

Problem:

You need to prompt the user of some event or state which requires immediate confirmation or a choice. For example, if the user tries to move away from a page with unsaved changes and you don't want to sae them automatically, you might prompt the user for what to do.

Solution:

Use an Alert Dialog or a DialogFragment. It permits giving suitable options to the user, in the case of unsaved changes scenario the options would be:

  1. Save
  2. Discard Changes
  3. Cancel.

Discussion:

Through the AlertDialog class, the user can be provided with 2 options that can be used in any scenario:

  1. Positive Reaction
  2. Neutral Reaction
  3. Negative Reaction.

If the user ahs entered some data in an EditText and is then attempting the cancel that activity, the application should prompt the user to either Save his changes, Discard them or calcel the alert dialog itself.

Android Up to 2.3.x

The recommended way to handle Dialogs in Android seems a bit round-about, but it allows Android to handle resource management. It is:

  1. Assign a small integer to each Dialog in your app;
  2. When you need a dialog displayed, call the Activity method showDialog() passing the appropriate int identifier for the dialog;
  3. The first time the dialog is shown, this will cause Android to call your onCreateDialog(), wherein you actually create and return the Dialog.
  4. Then, Android will call your onPrepareDialog(). You don't have to implement this; the inherited version is a no-op. You might use it to set the text to "This is attempt 9 of 10 before we self-destruct!", as an example.
  5. Then Android will actually show the Dialog.
  6. If you are sure you won't need the Dialog again, dismiss it.

Here is the code that would do the "reate the dialog" part, along with appropriate click listeners on each button on the dialog.


alertDialog = new AlertDialog.Builder(this)
.setTitle(R.string.unsaved)
.setMessage(R.string.unsaved_changes_message)
.setPositiveButton(R.string.save_changes, new AlertDialog.OnClickListener() {
	public void onClick(DialogInterface dialog, int which) {
		saveInformation();
	}
	})
.setNeutralButton(R.string.discard_changes, new AlertDialog.OnClickListener() {
	public void onClick(DialogInterface dialog, int which) {
	        finish();
        }
	})
.setNegativeButton(android.R.string.cancel_dialog, new AlertDialog.OnClickListener() {
	public void onClick(DialogInterface dialog, int which) {
	        dialog.cancel();
	}
	})
	.create();

If you are doing thing the recommended way, return the created AlertDialog from onCreateDialog() and you should be finished. If you are doing things the quick-and-dirty way, you need to call alertDialog.show(); to cause the dialog to appear.

Android 3.0 and later

Honeycomb introduced a new recommended way of doing dialogs with the aim of simplifying life. The new approach is as follows:

  1. Subclass DialogFragment
  2. Create a View (preferably using XML layout)
  3. In the Activity class, instantiate your DialogFragment subclass
  4. Get a reference to the system Dialog Manager
  5. Call your DialogFragment's show method, passing it the Dialog Manager and any optional arguments.

XXX CODE TO FOLLOW XXX

No records found.