Search code examples
androidandroid-navigationview

Unable to change the item text color and item icon tint


Im trying to change the text color and icon tint color of navigation view as follows,

<?xml version="1.0" encoding="utf-8"?>

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


        <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:background="@color/colorPrimary"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            app:itemIconTint="@android:color/white"
            android:theme="@style/AppTheme.NavigationView"
            app:itemTextAppearance="?android:attr/textAppearanceSmall"
            app:itemTextColor="@android:color/white"
            app:menu="@menu/drawer_view"/>

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

But this doesnt helped me to change the color..So I tried setting the color programmatically as follows,

   // FOR NAVIGATION VIEW ITEM TEXT COLOR
        int[][] state = new int[][]{
                new int[]{-android.R.attr.state_enabled}, // disabled
                new int[]{android.R.attr.state_enabled}, // enabled
                new int[]{-android.R.attr.state_checked}, // unchecked
                new int[]{android.R.attr.state_pressed}  // pressed

        };

        int[] color = new int[]{
                Color.WHITE,
                Color.WHITE,
                Color.WHITE,
                Color.WHITE
        };

        ColorStateList csl = new ColorStateList(state, color);


// FOR NAVIGATION VIEW ITEM ICON COLOR
        int[][] states = new int[][]{
                new int[]{-android.R.attr.state_enabled}, // disabled
                new int[]{android.R.attr.state_enabled}, // enabled
                new int[]{-android.R.attr.state_checked}, // unchecked
                new int[]{android.R.attr.state_pressed}  // pressed

        };

        int[] colors = new int[]{
                Color.WHITE,
                Color.WHITE,
                Color.WHITE,
                Color.WHITE
        };

        ColorStateList csl2 = new ColorStateList(states, colors);

        navigationView.setItemTextColor(csl);
        navigationView.setItemIconTintList(csl2);

Though I have tried setting the itemTextColor and itemIconTint to white programmatically as well as through xml, it is not effecting. Its still grey in color. How can I be able to sort this out?

UPDATE:

I have ofcourse tried setting the following theme to navigationview

<style name="AppTheme.NavigationView">
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:textColorSecondary">#FFFFFF</item>
    </style>

Solution

  • try to use Custom Color State drawable for changing menu item's text and icon color.

     app:itemIconTint="@color/custom_text_color" //icon color
    app:itemTextColor="@color/custom_text_color" //text color
    app:itemBackground="@drawable/menu_background_color" //background
    

    in res/color/custom_text_color.xml

        <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:color="@color/White" android:state_checked="true" />
      <item android:color="@color/Black" android:state_checked="false"/>
    </selector>
    

    Also in res/drawable/menu_background_color.xml

        <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@android:color/transparent"  android:state_checked="false"/>
      <item android:drawable="@color/colorPrimary" android:state_checked="true"/>
    </selector>