Wai Yan Hein
Wai Yan Hein

Reputation: 14831

How to change the text color of checked navigation view item in Android?

I am developing an Android app. In my app, I am using navigation view and drawer. What I want is I want to change the text color of checked item. I searched online and I found a link Change the color of a checked menu item in a navigation drawer . I followed it. But it is not working and just throwing error.

I also tried like this:

<android.support.design.widget.NavigationView
        app:itemIconTint="@color/red"
        app:itemTextColor="@color/colorAccent"
        app:itemBackground="@color/white"
        android:id="@+id/left_nv_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white" />

But the item text is never red even if it is checked. How can I achieve it? Actually, I want to customize the whole checked item.

Upvotes: 1

Views: 947

Answers (1)

Bharatesh
Bharatesh

Reputation: 9009

Try this:

res/color/menu_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--state is enabled and checked-->
    <item android:color="@color/red"  android:state_enabled="true" android:state_checked="true" />
    <!--state is enabled and not checked-->
    <item android:color="@color/black" android:state_enabled="true" android:state_checked="false" />
    <!--state (menu item) is disabled -->
    <item android:state_enabled="false" android:color="@color/light_black"  />
</selector>

set it like this

 <android.support.design.widget.NavigationView
        ........
        app:itemIconTint="@color/menu_selector"
        app:itemTextColor="@color/menu_selector"/>

UPDATED: If above code didn't work then try moving selector to drawable folder and refer the same in NavigationView.

Upvotes: 1

Related Questions