AlertDialog with selector

I am trying to make a dialog with a selector that looks something like this:

AlertDialog with selector

I tried using an AlertDialog that contains a ListView, but this gives an ugly black border between the ListView and the bottom gray area. I could use a regular dialog, but I do not want to create a lower gray space manually.

I know that I can subclass AlertDialog, but then I will also need to subclass Builder, and in the end it will be a lot of code for such a small detail. Is there a neat way to do this?

Greetings

+8
android android-dialog android-alertdialog
source share
2 answers

Use the constructor of the warning dialog box; it has options for this. A brief example:

AlertDialog.Builder adb = new AlertDialog.Builder(this); CharSequence items[] = new CharSequence[] {"First", "Second", "Third"}; adb.setSingleChoiceItems(items, 0, new OnClickListener() { @Override public void onClick(DialogInterface d, int n) { // ... } }); adb.setNegativeButton("Cancel", null); adb.setTitle("Which one?"); adb.show(); 

See the doc dialog, section Adding a List .

+28
source share

You must use the following code to select an individual item. This is working code.

 CharSequence colors[] = new CharSequence[]{"View PDF", "Reduce Size", "Delete PDF", "Share PDF"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Select Option"); builder.setItems(colors, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.e("value is", "" + which); switch (which) { case 0: break; case 1: break; case 2: break; case 3: break; } } }); builder.show(); 
+2
source share

All Articles