pgsandstrom
pgsandstrom

Reputation: 14399

AlertDialog with selector

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,

Upvotes: 12

Views: 20466

Answers (2)

SANJAY GUPTA
SANJAY GUPTA

Reputation: 1604

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();

Upvotes: 8

user658042
user658042

Reputation:

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.

Upvotes: 42

Related Questions