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?

- 5,753
- 72
- 57
- 129

- 1,051
- 5
- 15
- 29
4 Answers
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
-
1I've been hacking around with context menus for so long thinking they were the only solution... Thank you. This is both much easier, cleaner and visually similar to the context menu pattern – alexgophermix Aug 30 '16 at 16:51
If you really want to do it for whatever reason... (in my case, out of laziness)
During onCreate
of your activity or somewhere before your user can touch the button, do registerForContextMenu
on that button. Then in the actual button onClick handler, call openContextMenu(View)
.
For example, I have a button declared in xml like
<Button
android:id="@+id/btn_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onHelp"
android:text="@string/help_btn_text" />
in my onCreate
registerForContextMenu(findViewById(R.id.btn_help));
and in onHelp function
public void onHelp(View v) {
openContextMenu(v);
}
this works because the View v is the same as the view registered for context menu.

- 2,076
- 24
- 30
First thing, you should register the view by calling registerForContextMenu(View view). Second, override the onCreateContextMenu() to add the menus and lastly, override the onContextItemSelected() to put logic on each menu.

- 3,446
- 9
- 35
- 35
-
case context_menu: this.registerForContextMenu(getListView().getFocusedChild()); this.openContextMenu(getListView().getFocusedChild()); return true; This does not work for me. Any idea? – Mustafa İrer Apr 13 '11 at 10:17
First of all, you should know why you should use ContextMenu
. The functionality of ContextMenu
of a View is similar to the right-click menu on a PC, which means the "available operations" on some item.
According to your description, I think what you actually need is a customized Dialog with a list, which is displayed when clicking the Button and is also able to get the focused item of your ListView
. Then you can save the registration of ContextMenu
for some View that really needs the menu:)

- 5,283
- 6
- 37
- 63
-
Actually in my file manager application there is no scenerio that the user can handle long click. So that s why i m trying to do a button in options menu which handle the context menu. In my code i override the onCreateContextMenu() and onContextItemSelected() methods. and in my onOptionsItemSlelected method i have trying to call context menu. i tried case context_menu: this.registerForContextMenu(getListView().getFocusedChild()); this.openContextMenu(getListView().getFocusedChild()); return true; This does not work for me. Any idea? – Mustafa İrer Apr 13 '11 at 10:14