AlertDialog with selector

pgsandstrom picture pgsandstrom · Dec 22, 2011 · Viewed 19.2k times · Source

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,

Answer

user658042 picture user658042 · Dec 22, 2011

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.