How can I change separator color in NavigationView?
Asked Answered
H

6

44

I'm trying to use NavigationView to implement NavigationDrawer. I have added the separator by setting group id in menu. However I can't see the separator. I guess it is because the separator color is same as the background. So I want to change the separator color. But I find no way to change it. Can anyone help me?

In screenshot, it seems have a room for separator between History and Settings, but you can't see it.

enter image description here

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context=".ui.MapActivity">

    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar_actionbar" />

        <!--main content-->
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/navdrawer_background"
        app:insetForeground="#4000"
        app:itemTextColor="@color/navdrawer_item_text_color"
        app:itemIconTint="@color/navdrawer_item_icon_tint"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

drawer.xml

<?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"/>
        <item android:id="@+id/navdrawer_item_favourite"
            android:icon="@drawable/ic_drawer_fav"
            android:title="@string/navdrawer_item_fav"/>
        <item android:id="@+id/navdrawer_item_history"
            android:icon="@drawable/ic_drawer_history"
            android:title="@string/navdrawer_item_history"/>
    </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"/>
        <item android:id="@+id/navdrawer_item_help"
            android:icon="@drawable/ic_drawer_help"
            android:title="@string/navdrawer_item_help"/>
        <item android:id="@+id/navdrawer_item_about"
            android:icon="@drawable/ic_drawer_about"
            android:title="@string/navdrawer_item_about"/>
    </group>
</menu>
Hexagon answered 9/6, 2015 at 11:32 Comment(0)
L
104

just apply following line on style.xml

<item name="android:listDivider">your_color</item>

The below is just information for your knowledge ... If you have seen design support library .. they are using following layout for NavigationView seprator..

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="wrap_content">

    <View android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="?android:attr/listDivider"/>

</FrameLayout>

here, you can see android:background="?android:attr/listDivider" .. So enjoy ... and here is my output that i change color to holo_blue enter image description here

Lieb answered 9/6, 2015 at 11:58 Comment(16)
Any way of doing it programatically?Cuba
@Cuba No unfortunately there is no option for doing it programatically ... may be in future library it wil come ..Lieb
where to apply the below line <item name="android:listDivider">your_color</item>Scratchy
to your main style for eg. In AppthemeRadioactive
doesnt work for me, moreover the attributte doesnt accept color in hex format only the @color resource which is strangePrimm
@Lieb What if I want a divider between grouped items?Devon
@Devon follow this answer https://mcmap.net/q/273078/-android-navigationview-menu-group-divider-duplicate and see comment of Yassen Ahmed who want something like you and solved it.Lieb
@Lieb how to change divider color programatically. i tried but idid not get anysolution for that .Thanks in advance..:)Mascot
Is it possible to change divider color dynamically. Ofcourse yes but i didn't get any way for that :)Mascot
I tried it before a year. But i didn't find it. And for now i am away from my machine for 5 days. I can try after 5 days.Lieb
where i have to add frame layout i have added style but its not wokring can please suggest me @LiebCommutative
Is there any way to adjust margin/padding on the divider?Conjunctiva
this is bad solution. If you want to set it only for navigation view, than it doesnt work at all, if you set it for activity - it works, but it changes divider color in whole activity, for each your list.Unapproachable
@Unapproachable have you applied the style in which you mention the color ...?Lieb
Of course I did, but it just doesnt work :) You can ONLY change dividerColor for whole activity, this way it works (but its not good solution). But making custom style, and aplying it to NavigationView only, it doesnt workUnapproachable
Making custom style should also work... I don't know what's the issue with your code... Make sure you are not missing something.. I can only help when i can see your code ...Lieb
A
45

Here is best and easy way while using menu as view

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeToolbar.NavigationView"
    app:itemTextColor="@color/white"
    app:itemIconTint="@color/white"
    app:headerLayout="@layout/activity_home_nav_header"
    app:menu="@menu/activity_home_drawer" />

ThemeToolbar.NavigatinoView

 <style name="ThemeToolbar.NavigationView" >
    <item name="android:listDivider">@color/white</item>
    <item name="android:textColorSecondary">@color/white</item>
</style>

enter image description here

Anthologize answered 14/7, 2016 at 5:49 Comment(5)
What is the ThemeToolbar.?Aitch
It is any name of app theme, Here it is added just to associated NavigationView with ThemeToolbar so written as ThemeToolbar.NavigationView. NavigationView can be written as itself.Anthologize
where i have add style i have adeed and use same but not able to put divider can u please suggest me @O'oneCommutative
just add the style in your values file, styles.xml if this is the name you have given @MhanazSyed, or you can share your code if you still help on thisAnthologize
Do you need any help on this @UnapproachableAnthologize
A
13

Create a style in your styles.xml. Enter your preferred color in the android:listDivider

<style name="NavigationView"  parent="Base.Theme.AppCompat">
    <item name="android:listDivider">@color/text_lightgray</item>
</style>

Reference the style as the theme of your navigation view:

   <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="false"
    android:theme="@style/NavigationView"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view"
    app:itemTextColor="@color/text_white"/>

Lastly, make sure that the groups inside your menu have unique ids. If your groups do not have the id attribute, this won't work!

<group android:checkableBehavior="single"
    android:id="@+id/group1">
    <item
        android:id="@+id/item1"
        android:title="@string/item1" />
</group>
<group android:checkableBehavior="single"
    android:id="@+id/group2">

    <item
        android:id="@+id/item2"
        android:title="@string/item2" />

</group>
Aplanatic answered 7/5, 2018 at 11:2 Comment(0)
S
0

Perfect answer:

enter image description here

<android.support.design.widget.NavigationView
    android:id="@+id/activity_principal_nav_view"
    android:layout_width="@dimen/size_230"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/black"
    android:fitsSystemWindows="true"
    app:insetForeground="@color/white"
    app:menu="@menu/settings_menu"
    app:itemTextColor="@color/white"
    app:itemIconTint="@color/white"
    app:headerLayout="@layout/settings_menu_header"
    android:theme="@style/NavigationDrawerStyle"/>
Scarlatti answered 4/8, 2016 at 18:58 Comment(0)
V
0
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@color/menu_text_color</item>
    <item name="android:textColorSecondary">@color/menu_text_color</item>
</style>

textColorPrimary changes the color of the header.

Vanya answered 2/11, 2016 at 8:38 Comment(0)
G
0

Divider Color Is Changed

STEP 1:

Create list_divider.xml file in your drawable folder.

copy and paste the following lines into your list_divider.xml.

 <?xml version="1.0" encoding="utf-8"?> 
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/_16sdp"
android:insetRight="@dimen/_16sdp">
<shape>
    <solid android:color="#FFFFFF" />
    <size android:height="2dp" />
</shape>
</inset> 

STEP 2:

Add this line to your Main Theme ( Theme Mentioned in your AndroidManifest.xml file)

 <item name="android:listDivider">@drawable/list_divider</item>

For Example

<style 
     name="MyMaterialTheme.Base" 
     parent="Theme.AppCompat.Light.NoActionBar">

    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item> 
    <item name="android:listDivider">@drawable/list_divider</item>
    <item name="android:listDivider">@drawable/list_divider</item>

STEP 3: No Change required in NavigationView for divider Color

 <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="?attr/colorPrimary"
        app:headerLayout="@layout/header_navigation_drawer"
        app:itemIconTint="?attr/nav_drawer_icon_color"
        app:itemTextColor="?attr/nav_text_color"
        app:menu="@menu/navigation_drawer_list_menu"
        tools:ignore="MissingConstraints" /> 
Gush answered 7/7, 2022 at 14:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.