Search code examples
androidxmlandroid-navigationview

How do I give a submenu a custom backgroundcolor in a navigationview?


I'm trying to give a submenu a different color than the rest of my items in my navigationview, so I want my whole navigationdrawer to be white but the whole submenu that is nested in the navigationdrawer to be gray. I am using the standard android studio navigation drawer.

My code:

<group android:id="@+id/firstgroup">
    <item
        android:id="@+id/nav_kalender"
        android:icon="@drawable/ic_menu_kalender"
        android:title="@string/str_menu_kalender" />
    <item android:title="@string/str_menu_title_klassement">
        <menu>
            <item
                android:id="@+id/nav_indklassement"
                android:icon="@mipmap/ic_menu_indklassement"
                android:title="@string/str_menu_indklassement" />
            <item
                android:id="@+id/nav_clubklassement"
                android:icon="@drawable/ic_menu_clubklassement"
                android:title="@string/str_menu_clubklassement" />
            <item
                android:id="@+id/nav_nietverwerkteritten"
                android:icon="@drawable/ic_menu_nietverwerkteritten"
                android:title="@string/str_menu_nietverwerkteritten" />
            <item
                android:id="@+id/nav_reglement"
                android:icon="@drawable/ic_menu_reglement"
                android:title="@string/str_menu_reglement" />
            <item
                android:id="@+id/nav_prijzen"
                android:icon="@drawable/ic_menu_prijzen"
                android:title="@string/str_menu_prijzen" />
        </menu>
    </item>
</group>

If you look the example below, everything between the lines should have a light gray background color

enter image description here


Solution

  • NavigationMenuView navigationMenuView = (NavigationMenuView)navigationView.getChildAt(0);
    

    NavigationMenuView is extension of Recyclerview.So you can add a divider as ItemDecoration but you have to check for the position where divider needs to be added otherwise it gets added to all elements.

    Another approach is to use multiple groups instead of single group