Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Single Choice Items inside AlertDialog?

I haven't been able to set a Single Choice list, or a Multiple Choice List inside an AlertDialog.

I tried following the examples but I only get a Dialog with a Title, the Ok and Cancel buttons, and no list, and NO message (which I set!).

Here is the code:

    protected Dialog onCreateDialog(int id) {     switch (id) {     case DIALOG_DELETE_CITY:         CharSequence[] array = {"Red", "Blue", "Yellow"};          return new AlertDialog.Builder(ShowPypData.this)             .setTitle(R.string.city_actions_delete_label)             .setMessage(R.string.city_actions_delete_select_label)             .setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {                      @Override                     public void onClick(DialogInterface dialog, int which) {                         // TODO Auto-generated method stub                      }                 })             .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {                  @Override                 public void onClick(DialogInterface dialog, int which) {                     // TODO Auto-generated method stub                  }              })             .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {                  @Override                 public void onClick(DialogInterface dialog, int which) {                     dialog.dismiss();                  }             }).create();     default:         return null;     }  } 

The weird thing is that if I comment the setSingleChoiceItems part, I can see the message on the dialog.

like image 963
Julian Suarez Avatar asked Oct 22 '11 17:10

Julian Suarez


People also ask

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Is AlertDialog deprecated?

This method is deprecated.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

Seems that Buttons, Message and Multiple choice items are mutually exclusive. Try to comment out setMessage(), setPositiveButton() and setNegativeButton(). Didn't check it myself.

like image 163
slkorolev Avatar answered Sep 21 '22 00:09

slkorolev