0

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

ekad
  • 14,436
  • 26
  • 44
  • 46
  • Possible duplicate of [How to change color of text and icon of Sub-Menu attached to Navigation view?](http://stackoverflow.com/questions/32994329/how-to-change-color-of-text-and-icon-of-sub-menu-attached-to-navigation-view) – H Raval Mar 29 '16 at 10:34

3 Answers3

0
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

Ravi Theja
  • 3,371
  • 1
  • 22
  • 34
  • how would u give a group a custom background than ? – Mathias Cochet Mar 29 '16 at 11:27
  • you can override the `OnDraw` method of item decoration but you have to do it separately for each index if you wish to have different colors.I don't this think this would be a better solution.But you could try. – Ravi Theja Mar 29 '16 at 11:44
0

You can use a listview instead of using a menu in navigationdrawer like this. So that you can give any desired color in your xml file inflating in the adapter.

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

<include
    layout="@layout/app_bar_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <include layout="@layout/nav_header_main2"/>
        <ListView
            android:id="@+id/lst_menu_items"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:divider="@null"
            android:dividerHeight="0dp"/>
        </LinearLayout>
    <!--app:headerLayout="@layout/nav_header_main2"
    app:menu="@menu/activity_main2_drawer" >-->
    </android.support.design.widget.NavigationView>

Shahal
  • 1,008
  • 1
  • 11
  • 29
-1

like this.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group>
    <item
        android:id="@+id/nav_home"
        android:checkable="true"
        android:icon="@drawable/ic_dashboard"
        android:title="Home"/>
    <item
        android:id="@+id/nav_messages"
        android:checkable="true"
        android:icon="@drawable/ic_event"
        android:title="Messages"/>
    <item
        android:id="@+id/nav_friends"
        android:checkable="true"
        android:icon="@drawable/ic_headset"
        android:title="Friends"/>
    <item
        android:id="@+id/nav_discussion"
        android:checkable="true"
        android:icon="@drawable/ic_forum"
        android:title="Discussion"/>
</group>

<item android:title="Sub items">
    <menu>
        <item
            android:checkable="true"
            android:icon="@drawable/ic_dashboard"
            android:title="Sub item 1"/>
        <item
            android:checkable="true"
            android:icon="@drawable/ic_forum"
            android:title="Sub item 2"/>
    </menu>
</item>

</menu>
shingle
  • 19
  • 5