12

I am trying to do a Dialog with a selector that looks exactly like this:

AlertDialog with selector

I've tried using an AlertDialog that holds a ListView, but that gives an ugly black border between the ListView and the bottom gray area. I could use a normal Dialog, but I don't want to build the bottom gray area manually.

I know that I can subclass the AlertDialog, but then I will also need to subclass the Builder and it ends up being a lot of code for such a small detail. Is there any neat way of doing this?

Cheers,

pgsandstrom
  • 14,361
  • 13
  • 70
  • 104

2 Answers2

42

Use the alert dialog builder, it has options for that. Short 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 dialogs doc, section Adding a list.

Juuso Ohtonen
  • 8,826
  • 9
  • 65
  • 98
8

You Should Use following code to select single 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();
SANJAY GUPTA
  • 1,564
  • 13
  • 21