How to change the background color for each menu item in the design of the navigation drawer material?

advertisements

I use this design layout for NavigationDrawer and commun material design menu.xml but I cannot find a way to make each line of menuitem in different color. How to do it?

Here's my navigation drawer xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:id="@+id/drawer_layout"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemTextColor="#333"
        app:itemIconTint="#333"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>


AFAIK it isn't straightforward to do what you are upto. If you wan't such type of customisation for Navigation drawer, it would be better if you provide your entire view inside NavigationView and then you can style it in whichever way you want.

<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">

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:orientation="vertical"
            android:theme="@style/ThemeOverlay.AppCompat.Dark">

            ...

       </LinearLayout>
</android.support.design.widget.NavigationView>

Now, you can create your menu view inside this linear layout as the way you want. I hope this helps you..