Search code examples
androidnavigation-drawerandroid-navigationview

Navigation Drawer item divider with selected background color


I have a navigation drawer in my android app. Here's the code:

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:fitsSystemWindows="true"
    android:background="@color/white"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_menu"
    app:itemTextColor="@color/drawer_item"
    app:itemIconTint="@color/drawer_item"
    app:itemBackground="@drawable/nav_divider"/>

I've used one the answer to this post to add dividers between the items. Here's the nav_divider drawable that I'm using as itemBackground in my NavigationView:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="@dimen/activity_horizontal_margin">
        <shape android:shape="rectangle">
            <solid android:color="@color/light_gray"/>
        </shape>
    </item>
    <item android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
        </shape>
    </item>
</layer-list>

Now, the question is how do I change the background color of the selected item. I know how to do that without the divider, but with the divider, it's more complicated.

UPDATE:

Here's what I've tried:

I created another drawable called nav_divider_selected which is exactly like the nav_divider above except with different colors.

Then, I created a drawer_item_background drawable like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/nav_divider_selected" android:state_selected="true"/>
        <item android:drawable="@drawable/nav_divider"/>
</selector>

And, replaced the itemBackground of the NavigationView with this new selector (app:itemBackground="@drawable/drawer_item_background"). But, it didn't work. The items all look black, for some reason.


Solution

  • I got it. It was a simple issue. I was just using the wrong state. So, here's the solution. I created two different layer lists (to add the divider to the navigation items):

    nav_divider:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:left="@dimen/activity_horizontal_margin">
            <shape android:shape="rectangle">
                <solid android:color="@color/light_gray"/>
            </shape>
        </item>
        <item android:bottom="1dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/white"/>
            </shape>
        </item>
    </layer-list>
    

    nav_divider_selected:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:left="@dimen/activity_horizontal_margin">
            <shape android:shape="rectangle">
                <solid android:color="@color/light_gray"/>
            </shape>
        </item>
        <item android:bottom="1dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/light_blue"/>
            </shape>
        </item>
    </layer-list>
    

    Then, created a drawer_item_background drawable like this (state_checked should be used not state_selected):

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:drawable="@drawable/nav_divider_selected" android:state_checked="true"/>
            <item android:drawable="@drawable/nav_divider"/>
    </selector>
    

    And, then I used this drawable as itemBackground in my NavigationView:

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:fitsSystemWindows="true"
        android:background="@color/white"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu"
        app:itemTextColor="@color/drawer_item"
        app:itemIconTint="@color/drawer_item"
        app:itemBackground="@drawable/drawer_item_background"/>