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.AlertDialog.BuilderAlertDialog.
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.OnClickListenerDialogInterface.OnClickListener.OnCancelListener
set by the
builder
while the
OnDismissListener
is plugged into the finished dialog?
onDismiss.onCancel
and
onDismiss.onDismiss
and the dialog reappers in the new orientation.
Source code is in
AlertDialogSamples.java,alert_dialog.xml,strings.xml.
DIALOG_YES_NO_MESSAGE.
Two buttons.
DIALOG_YES_NO_LONG_MESSAGE.
Three buttons.
DIALOG_YES_NO_ULTRA_LONG_MESSAGE.
You can scroll the message up and down.
DIALOG_YES_NO_HOLO_LIGHT_MESSAGE.
Pass the theme
AlertDialog.THEME_HOLO_LIGHTR
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).android.Randroid.R.attr.alertDialogIconandroid-sdks/platforms/android-14/data/res/drawable-hdpi/ic_dialog_alert.png;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.pngres/drawable-hdpi
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);
android.R.string.dialog_alert_titlesetTitle
to
builder.setTitle("The Meaning of Life");
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.
++i == 1i == 2i == 3.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";