I have 1 activity, but would like to have multiple context menu’s for different UI components.
For example, I have a ListView which will react to:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Selection Options");
menu.add(0, v.getId(), 0, "Remove");
}
How can I create another context menu for the onClick event for an ImageView I have?
Actually, this method is to change the option menu dynamically. To create several context menus, you have to define them in your method
onCreateContextMenu. As you can see, this method receives a View as parameter, which is the View you clicked on to make the menu appear. So you keep the method you have for yourListView, and you add some conditions to differentiate yourViews. Then you use these conditions to create the wantedContext Menu.Note : Context Menus don’t support icons, so if you want icons, images or something similar you will have to either use an option menu you dynamically change, or create a custom menu with a custom view, intents and everything.