I have a navigation drawer like this image. I want to add a section separator (like the line separating Neptune). It seems simple but I can\'t find anything on the web that
You have two choices
To help you with some sample code, can you please post all the items you need in your menu ? We need to know exactly what will be static and what will be scrollable.
Edit: If you want that working with the exemple, get rid of the line
mDrawerList.setAdapter(new ArrayAdapter<String>(this, ...)
You need to supply a home made adapter like this: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
As i said in 2, in your adapter, you will have logic, and thus you can say in the getView() method
if(myPlanet.isNeptune())
holder.mSepatator.setVisibility(View.VISIBLE);
else
holder.mSepatator.setVisibility(View.GONE);
Add group in Menu file to add divider enter image description here
</item>
</group>
To separate menu items by a divider line, only group items with a unique id like following example:
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item
android:id="@+id/nav_apps_and_games"
android:icon="@drawable/ic_apps_black_24dp"
android:title="@string/my_apps_and_games" />
<item
android:id="@+id/nav_bookmarked_apps"
android:icon="@drawable/ic_add_bookmark_black_24dp"
android:title="@string/bookmarked_apps" />
<item
android:id="@+id/nav_manage_downloads"
android:icon="@drawable/ic_downloading_file_black_24dp"
android:title="@string/manage_downloads" />
<!-- SET A UNIQUE ID TO THE BELOW GROUP -->
<group android:id="@+id/group1">
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="@string/settings" />
<item
android:id="@+id/nav_sign_up"
android:icon="@drawable/ic_card_membership_black_24dp"
android:title="@string/sign_up_login" />
</group>
</menu>
Visual Result:
My hacky method is similar to Mostrapotski's.
In my Layout for my custom adapter, I'm adding a horizontal separator at the beginning of each item and setting it's visibility to gone.
For the elements that mark the beginning of a new group, I set their visibility to visible so that the separator shows up on top of it.
Make sure you define each group with a unique ID, separator won't appear without the ID.
For example, this is my drawer_menu.xml
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/menu_top"
android:checkableBehavior="single">
<item
android:checked="true"
android:id="@+id/drawer_item_timeline"
android:icon="@drawable/ic_timer_grey600_24dp"
android:title="@string/drawer_timeline"/>
<item
android:id="@+id/drawer_item_reports"
android:icon="@drawable/ic_timetable_grey600_24dp"
android:title="@string/drawer_reports"/>
</group>
<group
android:id="@+id/menu_bottom"
android:checkableBehavior="none">
<item
android:id="@+id/drawer_item_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="@string/drawer_settings" >
</item>
</group>
</menu>
Gabriel adds below in the comments that if the group doesn't have an id, the separator will not appear.
Add Group to add divider in navigation drawer
</item>
</group>