5

Is it possible to get the group id that a menu item is in?

I thought this would work, but getGroupId() always returns 0:

xml:

<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/menu_group">        
        <item android:id="@+id/edit"
              android:title="Edit" />

        <item android:id="@+id/delete"
              android:title="Delete" />               
    </group>              
</menu>

code:

@Override
public boolean onContextItemSelected(MenuItem item) {

    int groupId = item.getGroupId(); //always zero

    return super.onContextItemSelected(item);
}
Kris B
  • 3,436
  • 9
  • 64
  • 106

3 Answers3

3

call this method in onMenuItemSelected(int featureId, MenuItem item) rather than onContextItemSelected(MenuItem item), OptionMenu and ContextMenu are two different type menus in android

Jasonw
  • 5,054
  • 7
  • 43
  • 48
Longerian
  • 723
  • 5
  • 13
  • My code is in a fragment, not sure if I can use `onMenuItemSelected`. I'm running into the issue described here: http://stackoverflow.com/questions/5297842/how-to-handle-oncontextitemselected-in-a-multi-fragment-activity and wanted to avoid creating my menus in code (prefer to have them in XML). Was hoping I could wrap the menus in a group and detect the group id in `onContextItemSelected` – Kris B Apr 13 '12 at 01:54
  • I ended up following the code in that question I linked to. Kinda of lame, but doesn't seem to be a better way. – Kris B Apr 13 '12 at 03:20
2

I have tested it with Fragment, it works. look at the following code:

public class ContextMenuActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ContextMenuFragment content = new ContextMenuFragment();
    getSupportFragmentManager().beginTransaction().add(
            android.R.id.content, content).commit();
}

public static class ContextMenuFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_context_menu, container, false);
        registerForContextMenu(root.findViewById(R.id.long_press));
        return root;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        new MenuInflater(getActivity().getApplication()).inflate(R.menu.menu, menu);

        menu.add(777, 0, Menu.NONE, "Menu A");
        menu.add(777, 1, Menu.NONE, "Menu B");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                Log.i("ContextMenu", "Item 1a was chosen");
                Log.i("ContextMenu", "group: " + item.getGroupId());
                return true;
            case 1:
                Log.i("ContextMenu", "Item 1b was chosen");
                Log.i("ContextMenu", "group: " + item.getGroupId());
                return true;
            case R.id.edit:
                Log.i("ContextMenu", "Item Edit was chosen");
                Log.i("ContextMenu", "group: " + item.getGroupId());
                return true;
            case R.id.delete:
                Log.i("ContextMenu", "Item Delete was chosen");
                Log.i("ContextMenu", "group: " + item.getGroupId());
                return true;
        }
        return super.onContextItemSelected(item);
    }
}
}

`

<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/menu_group">        
        <item android:id="@+id/edit"
              android:title="Edit"
              android:orderInCategory="0"
              android:menuCategory="system"  />

        <item android:id="@+id/delete"
              android:title="Delete" 
              android:orderInCategory="0"
              android:menuCategory="system"  />               
    </group>              
</menu>
Longerian
  • 723
  • 5
  • 13
  • Yea, just seem lame you have to do the `menu.add` in code. Don't understand why the fragments can't automatically detect which menu to display. – Kris B Apr 13 '12 at 15:31
  • note that I do both adding menu in java code and inflating from xml, both are able to be selected and get their group id. :) – Longerian Apr 14 '12 at 01:59
0

Try this code - see the comments for details:

editTxt.setCustomSelectionActionModeCallback(new ActionMode.Callback(){

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu)
            {

                MenuItem [] menuitems=new MenuItem[]{};
                int i=menuitems.length;

               // menu.removeGroup(i);
                //or
               // menu.setGroupVisible(i,false);
                //or
                menu.setGroupVisible(i,false);


                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.copypaste, menu);


                return true;
            }
}
Adil B
  • 14,635
  • 11
  • 60
  • 78
An-Aide
  • 13
  • 3