For some reason I can’t create a context menu.
I have these lines in onCreate:
btnMenu = (ImageButton) findViewById(R.id.btnMenu);
registerForContextMenu(btnMenu);
And added the method:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
}
I have an xml file in res/menu called mainmenu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:title="item1"
/>
<item android:id="@+id/help"
android:title="item2"
/>
</menu>
But when I click the imagebutton nothing appears.
Any help is appreciated.
I’ve tried your code, and it worked great. Be careful, you need a long press on the view (in this example on the button), not a short click. If you want to show the context menu for a short click, try something like this: