1

I have an existing ListView, and when I long-click an Item I would like to see this kind of dialog:

enter image description here

For now I'm using a simple Android dialog with Horizontal buttons.

Is there a simple way to implement this behaviour and get the user result back (i.e. the user clicked "Edit" or "Delete"), or do I need to create a ListView activity again? The screen shot is from another application and I really don't know how the author implemented this.

Any sample code would be highly appreciated :)


I found this: Styling Text in a Dialog List

and This: http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList

and also this by @user3218281 (in the comments): Create a Context Menu when Click Long in a Custom ListView

Community
  • 1
  • 1
ZigiZ
  • 2,480
  • 4
  • 25
  • 41

1 Answers1

4

this may help you...

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("Edit");
    arrayList.add("Delete");
    arrayList.add("Cancel");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
            case 0:
                // Edit Clicked
                break;
            case 1:
                // Delete clicked
                break;
            case 2:
                // Cancel clicked
                break;
            default:
                break;
            }
        }
    };
    builder.setAdapter(adapter, listener);
    builder.show();
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • I have two questions: shouldn't there be a `.create()` before `builder.show`? i.e. `builder.create().show();`. also, is it possible to add an extra message below the title with this layout? builder.setTitle works, but adding builder.setMessage makes the list not show. – ZigiZ Jan 23 '14 at 12:33
  • 2
    @ZigiZ internally `show()` method will call `create()` method... and design a LinearLayout with orientation as vertical and add two TextViews in it. first one for showing Title and second TextView for showing message. and set this LinearLayout as custom title View using `builder.setCustomTitle(LinearLayout);` – Gopal Gopi Jan 23 '14 at 12:38
  • thanks! Do I create the LinearLayout at run time or do I use an XML for it or what? – ZigiZ Jan 23 '14 at 12:55
  • @ZigiZ It is of your choice friend... creating dynamically or using xml leads to same thing right... where you feel comfortable? – Gopal Gopi Jan 23 '14 at 12:57
  • I don't know yet :) what would you have choose? I'll google `setCustomTitle` and I'm sure I'll find an example, if not I'll post a question on SO :) Thank you so much! – ZigiZ Jan 23 '14 at 13:15