2

Given a NavigationView with a section with sub menu items like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:title="Sub items">
    <menu>
      <item android:title="Sub item 1" />
      <item android:title="Sub item 2" />
    </menu>
  </item>
</menu>

Question: How do I change the background color of the Sub items row?

In my experiments, it looks like the color used for the sub menu header row/item is the same as the background color of the NavigationView (android:backgroundTint and android:background) but I have not found a way to specify the two separately. I need the background color to be one color and the sub menu header row/item another color.

enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
infinite-loop
  • 882
  • 11
  • 17

2 Answers2

0

itemBackground, itemIconTint and itemTextColor are simple xml-attributes that can be set, though you have to use a custom prefix instead of the android: one.

Example

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:fitsSystemWindows="true">

    <!-- Other layout views -->

    <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"
        app:itemBackground="@drawable/my_ripple"
        app:itemIconTint="#2196f3"
        app:itemTextColor="#009688"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>

Example Create a new *.xml file in /res/color - let's name it state_list.xml - with the following content:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This is used when the Navigation Item is checked -->
    <item android:color="#009688" android:state_checked="true" />
    <!-- This is the default text color -->
    <item android:color="#E91E63" />
</selector>

and the simply reference it like this: app:itemTextColor="@color/state_list"

The same goes for itemIconTint. itemBackground expects a resource id.

Mr Robot
  • 1,747
  • 6
  • 35
  • 67
  • this gets us closer to the solution but not quite there. Yes, with this we are able to change the color of the sub menu **header** row/item but we cannot differentiate it from a sub menu row/item. The goal is to change the *header* row/item background color so that it looks different than one of it's sub menu row/items. I – infinite-loop Dec 15 '15 at 18:12
0

use compile 'com.android.support:appcompat-v7:23.1.1'

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main_navigation"
app:itemBackground="@drawable/nav_view_item_background"
app:itemTextColor="@color/nav_item_text_color"
app:menu="@menu/activity_main_navigation_drawer" />

uses to the following XML file for item backgroud: nav_view_item_background.xml

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

uses to the following XML file for Text Color: nav_item_text_color

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true" />
<item android:color="@android:color/black" />

Akash Jagtap
  • 404
  • 2
  • 8