1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • Have you tried a state list drawable? – Steve C. Mar 14 '16 at 00:23
  • I tried on the link I mentioned. I think that is what u want to point me out. I created color folder and added resource as link. But it throws error. – Wai Yan Hein Mar 14 '16 at 00:37
  • When using a state list drawable, you would create a xml file in your drawable folder and set that as the background. If you're setting the text for each item in the drawer programmatically then you can set the text color there as well by using texview.setColor(yourcolor); – Steve C. Mar 14 '16 at 00:40
  • also if you are getting anything in your logcat that might hint at what the problem is, please post your logcat. – Steve C. Mar 14 '16 at 00:41
  • Ok so you're using a Color State xml file. Could you post the Color State xml you're using? – Steve C. Mar 14 '16 at 00:44

1 Answers1

1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • It is not working. I found the solution. menu_selector must be in the drawable folder and so it will be like app:itemIconTint="@drawable/menu_selector" . Your one is the same as the link I mentioned. Thanks so much. – Wai Yan Hein Mar 14 '16 at 11:23
  • Your just edit your answer to drawable instead of color. I will upvote it. – Wai Yan Hein Mar 14 '16 at 11:23
  • Besides, how about background of checked item please ? – Wai Yan Hein Mar 14 '16 at 11:36
  • I wonder how it didn't work as color xml, as I am using the same thing where it works fine..Regarding changing background I never tried. – Bharatesh Mar 14 '16 at 13:16