0

Same code Same attribute , But different output in the drawer menu item ! I am using two emulator one of which is API level 19 and other one is API level 23 , I marked out the different view of my output . In the drawer menu i used a header name which color should be what i expected but it appear in API 23 device but not in API 19 , And most of all , how can i customize this header item by changing its color ( only the marked area not others )

<android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:background="#000"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/drawer_header_layout"
          app:itemIconTint="@color/colorPrimaryDark"
          app:itemTextColor="#FFFFFF"
          app:menu="@menu/drawer_item">

This is for menu item ...

 xmlns:android="http://schemas.android.com/apk/res/android">  
  <item
          android:id="@+id/essential"  
          android:checkable="false"
          android:title="Essential">
          <menu>
              <item
                  android:id="@+id/blood_group"
                  android:checkable="true"
                  android:icon="@drawable/blood"
                  android:title="Blood Group">

              </item>
              <item
                  android:id="@+id/respected_halls"
                  android:checkable="true"
                  android:icon="@drawable/home"
                  android:title="Respected Halls">

              </item>

https://i.stack.imgur.com/doaFM.png https://i.stack.imgur.com/nmWjq.png

2 Answers2

0

There are two background settings. See XML below.

    <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:background="@color/colorPrimaryDark"  <-- This changes what you want
    app:headerLayout="@layout/nav_header_main"
    app:itemBackground="@color/colorPrimaryDark" <-- This changes items bg
    app:itemTextColor="@color/mbar_default_foreground_color"
    app:itemIconTint="@color/mbar_default_foreground_color" <-- recolor icons
    app:menu="@menu/activity_main_drawer"/>

In short

android:background changes background of the header items

app:itemBackground the navigation items background

app:itemTextColor the foreground color for navigation items

app:itemIconTint the icon color

The menu items for this are organized like this: (This is just to help for the comments below)

    <item android:title="@string/main_menu_section_music">
    <menu>
        <item
            android:id="@+id/main_menu_item_live_in_concert"
            android:icon="@drawable/icon_main_menu_live"
            android:title="@string/main_menu_live"/>
        <item
            android:id="@+id/main_menu_item_playlists"
            android:icon="@drawable/icon_main_menu_playlists"
            android:title="@string/main_menu_playlists"/>
        <item
            android:id="@+id/main_menu_item_albums_lyrics"
            android:icon="@drawable/icon_main_menu_albums"
            android:title="@string/main_menu_albums"/>
        <item
            android:id="@+id/main_menu_item_youtube"
            android:icon="@drawable/icon_main_menu_youtube"
            android:title="@string/main_menu_youtube"/>
    </menu>
</item>

Cheers, hope it helps

Grisgram
  • 3,105
  • 3
  • 25
  • 42
  • Strange. I did exact this in my current app to re-color the items you showed in your picture. You are using a standard NavigationView, are you? And it's in a right? Post your full xml please. – Grisgram Feb 10 '17 at 16:58
  • I updated my answer with the menu structure I used for organizing the items. I am sorry, but this works. Just changed a color for fun (I am working on this app at this very moment) to yellow and my menu became yellow. – Grisgram Feb 10 '17 at 17:07
  • I include my XML in comment , Please have a look. –  Feb 10 '17 at 17:08
  • Please check it now . –  Feb 11 '17 at 15:48
0
 1. <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">

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

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#FFF"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/drawer_header_layout"
            app:itemIconTint="@color/colorPrimaryDark"
            app:itemTextColor="#000"
            app:menu="@menu/drawer_item">

        </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>
  • Ok - I am not sure about #FFF as color... does this really work? I know them as 6 (without alpha) or 8 (with alpha) in length. Never seen a FFF. If this works, ok, then I learned something, but I always do #FFFFFF for white, #FF0000 for red (in fact, I use @color resource and never hardcode colors, but anyway). Try with full qualified #FFFFFF please. – Grisgram Feb 10 '17 at 17:11
  • curious. I haven't seen 3 digit color codes before, but apparently they are a thing - http://stackoverflow.com/questions/3108860/how-to-use-3-digit-color-codes-rather-than-6-digit-color-codes-in-css. ... but not sure Android accepts them. – trooper Feb 10 '17 at 18:29