This question already has an answer here:
-
How to create a simple divider in the new NavigationView?
12 answers
Android support design library provide NavigationView:
<android.support.design.widget.NavigationView
...
app:menu="@menu/navigation_drawer_items" />
menu/navigation_drawer_items:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item .../>
...
</group>
<group android:checkableBehavior="single">
<item .../>
...
</group>
</menu>
How to add divider, separator or space between groups (like on picture)?
Just give a unique id to each group. It will create a separator automatically.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/group_feature"
android:checkableBehavior="single">
<item android:id="@+id/navdrawer_item_map"
android:checked="true"
android:icon="@drawable/ic_drawer_map"
android:title="@string/navdrawer_item_map"/>
</group>
<group android:id="@+id/group_settings"
android:checkableBehavior="single">
<item android:id="@+id/navdrawer_item_settings"
android:icon="@drawable/ic_drawer_settings"
android:title="@string/navdrawer_item_settings"/>
</group>
</menu>
you do something like this.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_dashboard"
android:title="Home" />
<item
android:id="@+id/nav_messages"
android:icon="@drawable/ic_event"
android:title="Messages" />
<item
android:id="@+id/nav_friends"
android:icon="@drawable/ic_headset"
android:title="Friends" />
<item
android:id="@+id/nav_discussion"
android:icon="@drawable/ic_forum"
android:title="Discussion" />
</group>
<item android:title="Sub items">
<menu>
<item
android:icon="@drawable/ic_dashboard"
android:title="Sub item 1" />
<item
android:icon="@drawable/ic_forum"
android:title="Sub item 2" />
</menu>
</item>
</menu>
this code is taken from the Design Library example app made by Chris Barnes https://github.com/chrisbanes/cheesesquare
I'm using it this way:
<group
android:id="@+id/group1"
android:checkableBehavior="single">
<item
android:id="@+id/menu1"
android:icon="@drawable/somedrawable"
android:title="Lorem ipsum" />
</group>
<group
android:id="@+id/group2"
android:checkableBehavior="single">
<item
android:id="@+id/menu2"
android:icon="@drawable/somedrawable"
android:title="Dolor it amet" />
</group>
IDs are important (+id/group1 and +id/group2). Without ids you won't see divider. It is working in my app.
You can add your text between <item android:title="title_name">
and </item>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_display_image"
android:title="Load Image"
android:icon="@drawable/ic_menu_gallery"
app:showAsAction="always"
app:actionLayout="@layout/switch_nav_drawer"/>
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Saved News" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
</group>
<item android:title="Settings">
<menu>
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</menu>
</item>
<item
android:id="@+id/about_us"
android:icon="@drawable/ic_info_black_24dp"
android:title="About Us"></item>
</menu>
Divider in NavigationMenuList achieved using grouping the Menu items but make sure you are giving group id @+id that is necessary.
Below is working code for you
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/gp_one"
android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_avatar"
android:title="Home" />
</group>
<group
android:id="@+id/gp_two"
android:checkableBehavior="single">
<item
android:id="@+id/nav_account"
android:icon="@drawable/ic_avatar"
android:title="My Account" />
<item
android:id="@+id/nav_orders"
android:icon="@drawable/ic_avatar"
android:title="My Orders" />
<item
android:id="@+id/nav_wishlist"
android:icon="@drawable/ic_avatar"
android:title="My Wishlist" />
</group>
<group
android:id="@+id/gp_three"
android:checkableBehavior="single">
<item
android:id="@+id/nav_rateus"
android:icon="@drawable/ic_avatar"
android:title="Rate Us" />
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_avatar"
android:title="Share" />
<item
android:id="@+id/nav_logout"
android:icon="@drawable/ic_avatar"
android:title="Logout" />
</group>