28

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();
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Alex Feh
  • 331
  • 3
  • 6
  • Where you have added the list items ? – GrIsHu Mar 21 '14 at 13:12
  • Why don't you put only "myAlertDialog.setTitle("Choose a color");" ? – JJ86 Mar 21 '14 at 13:15
  • @Alex Feh Check out my answer. – GrIsHu Mar 21 '14 at 13:16
  • @GrIsHu The List is in the var "items" as an CharSequence[] as you can see in line 5 of the code. The items are added in the first parameter of .setSingleChouceItems() in line 7. – Alex Feh Mar 21 '14 at 13:18
  • @JaAd This code only shows an example, in my code it would be great if i could use both. – Alex Feh Mar 21 '14 at 13:19
  • @JaAd yes, i get your point here. But i have an layout that requires this look, i know stupid. But if it doesn't work i have to live with it. The most thing that bothered me with this problem was that i wasted a lot of time not knowing that the .setMessage() was the problem why the list wasn't shown. Is there an convention or something for that? – Alex Feh Mar 21 '14 at 13:52
  • @AlexFeh the only think you can do is a custom Dialog with a title, a text (in your case "Choose a color.") and a list below the text. By the way, giacomino's answer is good and if you don't want to loose more time, replace "Options" with "Choose a color." ;-) . – JJ86 Mar 21 '14 at 14:28

5 Answers5

16

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.

Community
  • 1
  • 1
Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
11

AlertDialog creates its content dynamically by checking if mMessage that you set via setMessage() is null. If mMessage is NOT null (that means you have called setMessage() method), then it setups its content for ONLY TextView to show message and skips listview setup. If you haven't called setMessage(), then it removes TextView and then setups for listview.

Solution

So, to show both message and list in dialog, I suggest you to use below solution which setups custom view for AlertDialog.

        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Dialog title");

    final List<String> items = new ArrayList<>();

    items.add("Item 1");
    items.add("Item 2");
    items.add("Item 3");

    final ArrayAdapter<String> arrayAdapterItems = new ArrayAdapter<String>(
            getContext(), android.R.layout.simple_list_item_single_choice, items);

    View content = getLayoutInflater().inflate(R.layout.list_msg_dialog, null);

    //this is the TextView that displays your message
    TextView tvMessage = content.findViewById(R.id.tv_msg);

    tvMessage.setText("Dialog message!!!");

    //this is the ListView that lists your items
    final ListView lvItems = content.findViewById(R.id.lv_items);
    lvItems.setAdapter(arrayAdapterItems);
    lvItems.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    builder.setView(content);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //do something
            Toast.makeText(MainActivity.this,
                    items.get(lvItems.getCheckedItemPosition()),
                    Toast.LENGTH_SHORT).show();
        }
    });
    final AlertDialog dialog = builder.create();

    lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //when you need to act on itemClick
        }
    });


    dialog.show();

And this is list_msg_dialog.xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">

<TextView
    android:id="@+id/tv_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingTop="15dp"
    android:text="some text"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:textColor="@android:color/black"
    android:textSize="18sp" />

<ListView
    android:id="@+id/lv_items"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@null" /></LinearLayout>
Sirojiddin Komolov
  • 771
  • 10
  • 17
1

It's a little tricky, but you can use myAlertDialog.setCustomTitle(myTextView) and custom yout textView as title and text with HTML format.

TextView textView = new TextView(context);
textView.setText("YourTitle\n\n Your body");
myAlertDialog.setCustomTitle(textView);
-2

Try to set the items in your alertdialog as below:

  AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
  myAlertDialog.setTitle("Options");
  myAlertDialog.setMessage("Choose a color.");
  myAlertDialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
 CharSequence[] items = {"RED","BLUE","GREEN"};

 myAlertDialog.setItems(items, new DialogInterface.OnClickListener() {

 public void onClick(DialogInterface d, int choice) {

   }
  });
     ............
   myAlertDialog.create();
 myAlertDialog.show();
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
-2

You can make your alert dialog with list using the following method:

AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
myAlertDialog.setTitle("Options");
myAlertDialog.setMessage("Choose a color.");

List<String> items = new ArrayList<String>();   
items.add("RED");
items.add("BLUE");
items.add("GREEN");

final ArrayAdapter<String> arrayAdapterItems = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_expandable_list_item_1, items);

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.setAdapter(arrayAdapterItems,
new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        //do what you want
    }
});
myAlertDialog.show();

Hope it helps!

Giacomoni
  • 1,468
  • 13
  • 18
  • At the line myAlertDialog.setAdapter() i get an error that the var items has to be an ListAdapter and not a List. Did i do something wrong? I just copied your code. – Alex Feh Mar 21 '14 at 13:34
  • No, sry it was my mistake. You were getting error because it was trying to set the List instead the ListAdapter, that I forgot to create. I editted my answer, now it should works! – Giacomoni Mar 21 '14 at 13:37
  • 1
    Yes, now the code works. But unfortunately i have to remove .setMessage() to see the List. But thank you, i like the way with the adapter! – Alex Feh Mar 21 '14 at 13:47
  • 5
    This doesn't solve the problem, as above the message and items are mutually exclusive. – Andrew Kelly Feb 11 '18 at 13:15