How to add line separator for Android menu item

My menu item is getting larger, so I want to group them and make a line separator to separate each group. What should I do now?

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <!--group1--> <item android:id="@+id/action_addtag" android:title="@string/add_hashtag_string" app:showAsAction="never" /> <item android:id="@+id/action_block_list" android:title="Block" app:showAsAction="never" /> <item android:id="@+id/action_report_list" android:title="Report" app:showAsAction="never" /> <!--group2--> <item android:id="@+id/terms" android:title="Terms" app:showAsAction="never" /> <item android:id="@+id/feedback" android:title="FeedBack" app:showAsAction="never" /> <!--group3--> <item android:id="@+id/action_setting" android:title="Setting" app:showAsAction="never" /> </menu> 
+10
android drop-down-menu contextmenu menuitem menu
source share
2 answers

All you have to do is define a group with a unique identifier , I checked the implementation, if the group has a different identifier, it will create a separator.

Example menu that creates a separator:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <group android:id="@+id/grp1"> <item android:id="@+id/navigation_item_1" android:checked="true" android:icon="@drawable/ic_home" android:title="@string/navigation_item_1" /> </group> <group android:id="@+id/grp2"> <item android:id="@+id/navigation_item_2" android:icon="@drawable/ic_home" android:title="@string/navigation_item_2" /> </group> 

hope this helps

UPDATE

for the menu item maybe you can use this

  <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <item android:id="@+id/action_cart" android:title="cart" android:actionLayout="@layout/cart_update_count" android:icon="@drawable/shape_notification" app:showAsAction="always"/> </menu> 

and the actionLayout file will be

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <View android:id="@+id/divider" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/divider"/> <TextView android:id="@android:id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:gravity="center_vertical" android:textAppearance="?attr/textAppearanceListItemSmall"/> </LinearLayout> 
+19
source share

Be sure to call MenuCompat.setGroupDividerEnabled(menu, true); when you overestimate your menu, otherwise the groups will not be separated by a separator !

Example:

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_activity_main, menu); MenuCompat.setGroupDividerEnabled(menu, true); return true; } 

And make sure your xml menu has different groups, for example:

  <menu> <group android:id="@+id/sorting" > <item android:id="@+id/action_sorting_new_old" android:title="@string/action_sorting_new_old"/> <item android:id="@+id/action_sorting_a_z" android:title="@string/action_sorting_a_z"/> </group> <group android:id="@+id/settings"> <item android:id="@+id/action_settings" android:title="@string/action_settings"/> </group> </menu> 
+11
source share

All Articles