Reputation: 8768
Is it possible to add a separator between menu items in Android's context menu? I don't see any directions for this in the documentation. Apparently menu items should be separated in some cases when they perform operations of a different kind.
NB. The question is about context menu, not options menu.
Upvotes: 4
Views: 5497
Reputation: 21
I'm not able to comment and maybe I'm a bit late but I hope this will help someone. For what I know: Android adds a separator between groups if you enable this via ContextMenu.setGroupDividerEnabled.
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderTitle(R.string.choose_action);
menu.add(0, MENU_ITEM_CREATE, 0, R.string.create);
menu.add(1, MENU_ITEM_CHECK, 0, R.string.check);
menu.add(2, MENU_ITEM_EDIT, 0, R.string.edit);
menu.add(2, MENU_ITEM_DELETE, 1, R.string.delete);
// Enable group separator
menu.setGroupDividerEnabled(true);
}
https://developer.android.com/reference/android/view/Menu#setGroupDividerEnabled(boolean)
Upvotes: 2
Reputation: 8768
First I thought of only one workaround - a custom implementation of a context menu, such as Icon Context Menu for example. Such a code allows for extending menu item class to a specific menu separator class with custom view.
... But some time later I have found that ...
Another (much easier) solution could be adding a menu item with a row of '_' (underline) characters (surprisingly this is the only symbol in standard Android font which multiple instances can be shown smoothly side by side without gaps), and then aligning the item text in Java code using SpannableString
.
String resource:
<string name="hr">______________________________</string>
Adjust the string length as appropriate.
Menu layout:
<group android:checkableBehavior="none" android:enabled="false">
<item android:id="@+id/menu_gap"
android:title="@string/hr"
android:enabled="false" />
</group>
Java:
private void alignCenter(MenuItem item)
{
SpannableString s = new SpannableString(item.getTitle());
s.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, s.length(), 0);
item.setTitle(s);
}
Upvotes: 3