Change Navigation View Item Color Dynamically Android

I’d like to build a navigation drawer where each item has a different selection color (the icon tint and text color) as the google play store has:

enter image description here

  • retrieve original adapter from expandablelistview with header
  • Lollipop: Disabled button --> which style?
  • How to mask an EditText to show the dd/mm/yyyy date format
  • Removable storage (external) sdcard path by manufacturers
  • onKeyListener not working with soft keyboard (Android)
  • getOwnerActivity returns null in custom dialog
  • I’m not sure how they’ve solved this, I think they use different activities with different drawers. I want to use fragments and I want to change the icon tint and text color. Any ideas how I can do this? I’m using google’s design support library and a drawer layout with a navigation view in there.

    Related posts:

    How do you validate the format and values of EditTextPreference entered in Android 2.1?
    How to run activity in background in Android
    START_STICKY, foreground Android service goes away without notice
    Passing arguments to AsyncTask, and returning results
    Dynamically adding and removing table rows - Android
    ViewPager doesn't always refresh when setAdapter is called, FragmentStatePagerAdapter
  • SurfaceTexture methods not always called on Android 7.0
  • Disadvantages of using Basic4Android?
  • java.lang.IllegalStateException: The specified child already has a parent
  • how to Trigger broadcast receiver from notification
  • Getting Started with WiFi on Android
  • Questions about Android application update
  • 4 Solutions collect form web for “Change Navigation View Item Color Dynamically Android”

    use app:itemIconTint in your NavigationView for icons and use app:itemTextColor for textColors

    Sample :

    drawable/navigation_text_color :

    <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 layout :

    <android.support.design.widget.NavigationView
           .
           .
           app:itemTextColor="@drawable/navigation_text_color"/>
    

    If by dynamically you mean programmatically you could try this:

    // FOR NAVIGATION VIEW ITEM TEXT COLOR
    int[][] states = new int[][]{
            new int[]{-android.R.attr.state_checked},  // unchecked
            new int[]{android.R.attr.state_checked},   // checked
            new int[]{}                                // default
    };
    
    // Fill in color corresponding to state defined in state
    int[] colors = new int[]{
            Color.parseColor("#747474"),
            Color.parseColor("#007f42"),
            Color.parseColor("#747474"),
    };
    
    ColorStateList navigationViewColorStateList = new ColorStateList(states, colors);
    
    // apply to text color
    navigationView.setItemTextColor(navigationViewColorStateList);
    
    // apply to icon color
    navigationView.setItemIconTintList(navigationViewColorStateList);
    

    So you could define multiple colors for different settings like Day or Night.

    Finllay i got the answer,

    You must change the colorAccent in the colors file to which ever color you want :

      <color name="colorAccent">whichever color required</color>
    

    This solution worked for me

        <android.support.design.widget.NavigationView
               .
               .
               app:itemIconTint="@color/your_selector_file_name" // for Icon
            app:itemTextColor="@color/your_selector_file_name" // for text
    />
    

    Adding to what @DAVOOD said. The below color selector worked for me.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- This is used when the Navigation Item is checked -->
            <item android:color="@color/tenoBrandColor" android:state_pressed="true" />
            <!-- This is the default text color -->
            <item android:color="@color/textprimary" />
    </selector>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.