Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Item separator for context menu in Android

Tags:

android

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.

like image 591
Stan Avatar asked Sep 06 '25 16:09

Stan


1 Answers

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);
}
like image 119
Stan Avatar answered Sep 10 '25 00:09

Stan