Alex Feh
Alex Feh

Reputation: 341

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

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();

Upvotes: 29

Views: 8020

Answers (5)

David Cohen
David Cohen

Reputation: 59

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);

Upvotes: 1

Sirojiddin Komolov
Sirojiddin Komolov

Reputation: 791

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>

Upvotes: 11

Akeshwar Jha
Akeshwar Jha

Reputation: 4576

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.

Upvotes: 16

Giacomoni
Giacomoni

Reputation: 1468

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!

Upvotes: -2

GrIsHu
GrIsHu

Reputation: 23638

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();

Upvotes: -2

Related Questions