views:

51

answers:

2

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?

A: 
@Override
public boolean onPrepareOptionsMenu(Menu menu) 
{
    // TODO Auto-generated method stub
    return super.onPrepareOptionsMenu(menu);
}

You can check your conditions within this method. This will get fired before the menu is visible to user.

Rahul
what condition would i check?
Sheehan Alam
keep a flag or something if the user had clicked the imageview if yes then display your udpated menu there else display the normal one
Rahul
@Rahul: the OP asked about context menus -- your method is for option menus.
CommonsWare
+3  A: 

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 your ListView, and you add some conditions to differentiate your Views. Then you use these conditions to create the wanted Context 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.

Sephy
how can I determine if v is a ListView or if v is an ImageView?
Sheehan Alam
in Android, you can add tags to the views, so you just need to use getTag on the View. You can also use the id of the View if you know it.
Sephy