Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog: Why can't I show Message and List together?

Tags:

Does anybody know why the AlertDialog doesn't show the list of items when I add a Message with .setMessage()? The negative and positive buttons will be shown, but not the list. When I delete the line with .setMessage() everything works.

This is my code:

AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity()); myAlertDialog.setTitle("Options"); myAlertDialog.setMessage("Choose a color.");  CharSequence[] items = {"RED", "BLUE", "GREEN" };  myAlertDialog.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {      @Override     public void onClick(DialogInterface dialog, int which) {         // do stuff     } });  myAlertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {      @Override     public void onClick(DialogInterface dialog, int which) {        // do stuff     } });  myAlertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {      @Override     public void onClick(DialogInterface dialog, int which) {        // do stuff     } });  myAlertDialog.create(); myAlertDialog.show(); 
like image 562
Alex Feh Avatar asked Mar 21 '14 13:03

Alex Feh


People also ask

How do I show a list in alerts?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

How can I display a list view in an Android alert dialog?

Navigate to the app > res > layout and create a new layout file. Add a ListView as shown below. This layout would be displayed inside the AlertDialog.

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

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 .


1 Answers

From the docs,

Because the list appears in the dialog's content area, the dialog cannot show both a message and a list and you should set a title for the dialog with setTitle().

The setMessage() and setSingleChoiceItems() are therefore mutually exclusive.

like image 122
Akeshwar Jha Avatar answered Jan 13 '23 01:01

Akeshwar Jha