Create multiple context menus in one activity

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        
}

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);
    }

Tags:

Android