Reputation: 3578
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);
}
Upvotes: 5
Views: 8419
Reputation: 13
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;
}
}
Upvotes: 0
Reputation: 723
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>
Upvotes: 2
Reputation: 723
call this method in onMenuItemSelected(int featureId, MenuItem item)
rather than onContextItemSelected(MenuItem item)
, OptionMenu and ContextMenu are two different type menus in android
Upvotes: 3