Tutorial. I was able to give the dialog an icon and a title. But when I gave it a message, the list of items disappeared.
Source code is in
AlertDialogSamples.java
alert_dialog.xml
,strings.xml
.
DIALOG_LIST
.
Shows
a little dialog with a
message
when you
click
on an item.
DIALOG_SINGLE_CHOICE
.
Radio buttons with Cancel and OK at the bottom.
DIALOG_MULTIPLE_CHOICE
.
Check boxes with Cancel and OK at the bottom.
static final int checkedItem = 3; //Rearden metal is already checked. int which = checkedItem; //which item is currently checkedChange the call to
setItems
to the following.
See
builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ((ListDialogActivity)((Dialog)dialog).getOwnerActivity()).which = which; textView.setText("Thank you for considering " + items[which] + "."); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { textView.setText(""); } }); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int i = ((ListDialogActivity)((Dialog)dialog).getOwnerActivity()).which; textView.setText("Thank you for selecting " + items[i] + "."); } });
setSingleChoiceItems
to the following.
You’ll also have to replacethe
which
method of the activity with an array of booleans.
boolean[] checkedItems = {true, false, false, true}; builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { ((ListDialogActivity)((Dialog)dialog).getOwnerActivity()).which = which; textView.setText("Thank you for considering " + items[which] + "."); } else { textView.setText(""); } } });