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();
Try to set the items in your alertdialog as below:
AlertDialog
creates its content dynamically by checking ifmMessage
that you set viasetMessage()
isnull
. IfmMessage
is NOTnull
(that means you have calledsetMessage()
method), then it setups its content for ONLYTextView
to show message and skipslistview
setup. If you haven't calledsetMessage()
, then it removesTextView
and then setups forlistview
.Solution
So, to show both message and list in dialog, I suggest you to use below solution which setups custom view for AlertDialog.
And this is
list_msg_dialog.xml
layout:From the docs,
The
setMessage()
andsetSingleChoiceItems()
are therefore mutually exclusive.You can make your alert dialog with list using the following method:
Hope it helps!