How to add line divider for menu item Android
All you need to do is define a group with an unique ID, I have checked the implementation if group has different id's it will create a divider.
Example menu, creating the 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 menu item may be 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 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>
Make sure to call MenuCompat.setGroupDividerEnabled(menu, true);
when you inflate your menu, otherwise groups will not be separated by divider!
Example:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
MenuCompat.setGroupDividerEnabled(menu, true);
return true;
}
And make sure to have different groups in your menu xml, e.g.:
<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>
If you need add vertical lines in the Overflow menu (the ellipses at far right):
Assuming you have added a new menu resource file, do the following.
Group menu items and assign a unique ID to each group
<group android:id="@+id/group1"> <item android:id="@+id/action_about" android:orderInCategory="100" android:title="@string/about" app:showAsAction="never" /> </group> <group android:id="@+id/group2"> <item android:id="@+id/action_settings" android:orderInCategory="200" android:title="@string/settings" app:showAsAction="never" /> </group>
When you inflate the menu in the Activity's oncreate method:
getMenuInflater().inflate(R.menu.main, menu);//inflate menu MenuCompat.setGroupDividerEnabled(menu, true);//add horizontal divider
Old question, but the above answers didn't work for me (and I object to adding "groups" for single items). What did work was adding a style element as follows:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> <!-- .Light.DarkActionBar"> -->
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">#17161B</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>//<-add this
</style>
that references
<style name="PopupMenuListView" parent="@style/Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#dddddd</item>
<item name="android:dividerHeight">1dp</item>
</style>
in the same res/values/styles.xml file. Hope it helps!