Reputation: 4023
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.
Upvotes: 2
Views: 3156
Reputation: 2535
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:
Button b = (Button) findViewById(R.id.button1);
registerForContextMenu(b);
b.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
v.showContextMenu();
}
});
Upvotes: 7