Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple context menus in one activity

Tags:

android

Hi I'm trying to make multiple context menus in one activity. There are two buttons, and I want to create different context menu for the each buttons. Could anyone advise me or refer me to some working examples?

Here is my current code looks like.

registerForContextMenu(btn_1);
registerForContextMenu(btn_2);


btn_1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        openContextMenu(v);
    }   
});

btn_2.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        openContextMenu(v);
    }   

});


    }

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.media_menu, menu);
        menu.setHeaderTitle("Context menu for btn_1");
        menu.setHeaderIcon(R.drawable.icon_media_up); 
}

@Override
public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.photo:
                Toast.makeText(this, "One", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.video:
                Toast.makeText(this, "Two", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.audio:
                Toast.makeText(this, "Three", Toast.LENGTH_SHORT).show();
                return true;
            }
        return super.onContextItemSelected(item);
    }
like image 446
user1781367 Avatar asked Apr 24 '13 15:04

user1781367


People also ask

What is the difference between context menu and popup menu?

The contextual action mode displays action items that affect the selected content in a bar at the top of the screen and allows the user to select multiple items. See the section about Creating Contextual Menus. A popup menu displays a list of items in a vertical list that's anchored to the view that invoked the menu.

What is context menu class 3?

A context menu is a pop-up menu that provides shortcuts for actions the software developer anticipates the user might want to take. In a Windows environment, the context menu is accessed with a right mouse click.

What are context menu commands?

A context menu offers a list of relevant commands that apply to the current selection or task. The pen user can select commands near the selected object rather than move his or her hand across the screen to the menu or toolbar.

What is context menu explain with example?

In Android, the context menu is like a floating menu and arises when the user has long pressed or clicks on an item and is beneficial for implementing functions that define the specific content or reference frame effect. The Android context menu is alike to the right-click menu in Windows or Linux.


2 Answers

you have to handle this in your onCreateContextMenu method. there is a parameter View which is the view you have clicked. So by using that parameter you can inflate different menus. For example

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if(v.getId() == R.id.first_button)
             inflate one menu
        else if(v.getId() == R.id.second_button)
             inflate another menu        
}
like image 144
stinepike Avatar answered Sep 30 '22 20:09

stinepike


Here is the working solution

 @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);

     MenuInflater menuiflatr=getMenuInflater();
         if(v.getId() == R.id.ChooseSession){


        menuiflatr.inflate(R.menu.session_menu, menu);
         menu.setHeaderTitle("Choose Session Type");
         menu.setHeaderIcon(R.drawable.ic_launcher);
         MenuItem item_individual=menu.findItem(R.id.individual);
         MenuItem item_group=menu.findItem(R.id.group);
         if(itemselection_for_sessiontype==1)item_individual.setChecked(true);
         else if(itemselection_for_sessiontype==2)item_group.setChecked(true);
    }
        else if(v.getId() == R.id.ChooseTrajectory){
             //inflate another menu     
             menuiflatr.inflate(R.menu.trajectory_menu, menu);
             menu.setHeaderTitle("Enable Trajectory");
             menu.setHeaderIcon(R.drawable.ic_launcher);
             MenuItem item_On=menu.findItem(R.id.on);
             MenuItem item_Off=menu.findItem(R.id.off);
             if(itemselection_for_trajectory==1)item_On.setChecked(true);
             else if(itemselection_for_trajectory==2)item_Off.setChecked(true);
    }
        else if(v.getId() == R.id.ChooseMapeType){
            //inflate another menu     
            menuiflatr.inflate(R.menu.maptype_menu, menu);
            menu.setHeaderTitle("Choose Map Type");
         menu.setHeaderIcon(R.drawable.ic_launcher);
       }
   }`
  `
     @Override
     public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()){
        case R.id.individual:
            Toast.makeText(this, "Session Type Individual selected!",     `   `Toast.LENGTH_SHORT).show();
            item.setChecked(true);
            itemselection_for_sessiontype=1;
            return true;

    case R.id.group:
        Toast.makeText(this, "Session Type Group selected!", Toast.LENGTH_SHORT).show();
        item.setChecked(true);
        itemselection_for_sessiontype=2;
        return true;

    case R.id.on:
        Toast.makeText(this, "Trajectory Mode Enabled", Toast.LENGTH_SHORT).show();
        item.setChecked(true);
        itemselection_for_trajectory=1;
        return true;

    case R.id.off:
        Toast.makeText(this, "Trajectory Mode Disabled!",               Toast.LENGTH_SHORT).show();
        item.setChecked(true);
        itemselection_for_trajectory=2;
        return true;
    }





    return super.onContextItemSelected(item);
    }

    public void selectSession(View view){
        registerForContextMenu(view);

        openContextMenu(view);
    }
like image 20
Alpha Avatar answered Sep 30 '22 20:09

Alpha