Android opening context menu after button click

Mustafa İrer picture Mustafa İrer · Apr 13, 2011 · Viewed 37.6k times · Source

I want to open context menu when I click a button, but also I have to know which list item is focused when I click the button. Do you know how to do that? What code should be in onclick method?

Answer

dikirill picture dikirill · May 26, 2011

I was looking for the same, and found that instead of context menu, you should use Dialogs

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();
alert.show();

http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog