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();
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.
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.
A simple dialog containing an DatePicker . This class was deprecated in API level 26.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With