@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my_menu, menu); MenuItem item1 = menu.findItem(R.id.item1); // Set the group ID of item1 item1.setGroupId(R.id.group1); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { int groupId = item.getGroupId(); // Do something with the group ID, such as switch on it to determine an action switch (groupId) { case R.id.group1: // Handle group1 action break; case R.id.group2: // Handle group2 action break; } return true; }This example gets the group ID of a selected menu item (item) in the onOptionsItemSelected() method. The group ID is then used in a switch statement to determine what action to take based on the selected menu item's group. The android.view.MenuItem class is part of the Android Framework, which is included in the android package library.