56

I create an AlertDialog with an AlertDialog.Builder and set some items with setItems(). The dialog is shown but I cannot see any of the items. All I see is the message.

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
dialogBuilder.setMessage("Pick a color");
dialogBuilder.setItems(items, new DialogInterface.OnClickListener() {        
    public void onClick(DialogInterface dialog, int which) {
        // Do anything you want here
    }    
});

dialogBuilder.create().show();

If I set the PositiveButton, I can see that button just fine. I also tried setting MultiChoiceItems and SingleChoiceItems but neither of these work either.

j.f.
  • 3,908
  • 2
  • 29
  • 42
Allan Mermod
  • 806
  • 1
  • 8
  • 16

8 Answers8

192

Use setTitle instead of setMessage which sets message body and overrides the items list.

Paweł Nadolski
  • 8,296
  • 2
  • 42
  • 32
  • 4
    quite difficult to spot this when using list items along with alert dialog. – Zoombie Feb 01 '15 at 11:46
  • 2
    How can one have both? Items and message? – Pierre Feb 17 '17 at 11:42
  • 1
    @Armando, it's kind of hidden and may not have been there the whole time, but it's stated in the [Adding a List](https://developer.android.com/guide/topics/ui/dialogs.html#AddingAList) section of the dialog page: "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()." – j.f. Apr 13 '17 at 19:15
  • @wow thanks. If the API has this limitation the linter should be able to warn you in the same way it is done with other Android specific features. – tomacco Dec 09 '19 at 15:12
2

Why don't you go for setTitle instead of the setMessage? Try with setTitle("Pick a color").

I hope it will help you.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
2

Try alertDialogBuilder.setCustomTitle(view)

Dipendra
  • 1,547
  • 19
  • 33
2

If you want to set a message AND items, just use setCustomTitle() with a TextView like so:

dialogBuilder.setCustomTitle(TextView(context).apply {
    setPadding(
       16.dpToPx().toInt(),
       16.dpToPx().toInt(),
       16.dpToPx().toInt(),
       0
    )
    setText(it.message)
    setTextColor(Color.BLACK)
    textSize = 18f
})
dialogBuilder.setItems(...)

Note, that you cannot set a title this way, but of course you could create a custom title layout which support setting both the title AND the message.

dpToPx() is just a simple conversion extension function and not in the scope of your question, but here it is for completeness sake:

@JvmOverloads
@Dimension(unit = Dimension.PX)
fun Number.dpToPx(
    metrics: DisplayMetrics = Resources.getSystem().displayMetrics
): Float {
    return toFloat() * metrics.density
}
ubuntudroid
  • 3,680
  • 6
  • 36
  • 60
0

Use Below Code:-

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
dialogBuilder.setTitle("Pick a color");
dialogBuilder.setItems(items, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        // Do anything you want here
    }

});
dialogBuilder.create().show();
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0

try this

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
dialogBuilder.setTitle("Pick a color");
dialogBuilder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which) 
{
}
});
dialogBuilder.show();
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • `setSingleChoiceItems` is essentially the same as `setItems`: it's still exclusive with `setMessage`. The only difference is if you're implementing a persistent selection a radio button is shown for "single choice". – TWiStErRob Dec 13 '14 at 22:21
0

If you are using a resource string array you must include the resource packaging. context().getResources().getStringArray(R.array.items);

My list was not showing by using the R.array.items until i gave the pointer the context and resource packaging.

Good luck!

0

Use setView(View) with ListView if you want to achieve the effect of displaying a dialog with title, message and list of items at the same time.

Moz
  • 109
  • 2
  • 10