Changing text color of menu item in navigation drawer

Changing text color of menu item in navigation drawer

Asked on January 9, 2019 in XML.
Add Comment


  • 6 Answer(s)

         The NavigationView has a method called setItemTextColor(). It uses a ColorStateList.

    // 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);
    

         And then right after assigning the NavigationView:

    if (nightMode == 0) {
          navigationView.setItemTextColor(csl);
          navigationView.setItemIconTintList(csl2);
       }
    
    Answered on January 9, 2019.
    Add Comment

    Try the allternative code:

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:background="#000"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:itemTextColor="your color"
        app:menu="@menu/drawer" />
    
    Answered on January 9, 2019.
    Add Comment

         Use the app:itemIconTint in the NavigationView,

    <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:itemTextColor="@color/customColor"
       app:itemIconTint="@color/customColor"
       android:fitsSystemWindows="true"
       app:headerLayout="@layout/nav_header_home"
       app:menu="@menu/activity_home_drawer" />
    
    Answered on January 9, 2019.
    Add Comment
    Android Navigation Drawer
    1. Create a new Android application using application name: “Navigation Drawer” and package name: “com.javahelps.navigationdrawer“.
    2. Select the “Navigation Drawer Activity” as the default activity.
    3. Step 3: (Optional) …
    4. To change the default theme color of the application, change the values in values/colors.xml file.
    Answered on February 25, 2019.
    Add Comment

    <?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout 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:layout_width=“match_parent” android:layout_height=“match_parent” android:paddingBottom=“@dimen/activity_vertical_margin” android:paddingLeft=“@dimen/activity_horizontal_margin” android:paddingRight=“@dimen/activity_horizontal_margin” android:paddingTop=“@dimen/activity_vertical_margin” app:layout_behavior=“@string/appbar_scrolling_view_behavior” tools:context=“com.javahelps.navigationdrawer.HomeFragment”> <TextView android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“Home” /> <android.support.design.widget.FloatingActionButton android:id=“@+id/fab” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:layout_alignParentBottom=“true” android:layout_alignParentRight=“true” android:layout_margin=“@dimen/fab_margin” android:src=“@android:drawable/ic_input_add” /> </RelativeLayout

    Answered on February 25, 2019.
    Add Comment

    <?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout 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:layout_width=“match_parent” android:layout_height=“match_parent” android:paddingBottom=“@dimen/activity_vertical_margin” android:paddingLeft=“@dimen/activity_horizontal_margin” android:paddingRight=“@dimen/activity_horizontal_margin” android:paddingTop=“@dimen/activity_vertical_margin” app:layout_behavior=“@string/appbar_scrolling_view_behavior” tools:context=“com.javahelps.navigationdrawer.SettingsFragment”> <TextView android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“Settings” /> </RelativeLayout>

    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.