NavigationView menu item color does not change on API 15
I am new to StackOverFlow and this is my first question. Forgive me if I am wrong. I have NavigationView
inside DrawerLayout
that looks something like this:
DrawerLayout
<?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/app_bar_home_screen"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
android:background="@android:color/white"
app:itemBackground="@drawable/nav_item_selector"
app:itemIconTint="@color/nav_icon_color"
app:itemTextColor="@color/nav_text_color"
app:headerLayout="@layout/nav_header_home_screen"
app:menu="@menu/activity_home_screen_drawer">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Below are my classes nav_item_selector, nav_icon_color, nav_text_color
nav_item_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/fb_color" android:state_pressed="true"/>
<item android:drawable="@android:color/white" android:state_pressed="false"/>
</selector>
nav_icon_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_pressed="true" />
<item android:color="#2e9f68" android:state_pressed="false" />
</selector>
nav_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#b5edd2" android:state_pressed="true" />
<item android:color="#222222" android:state_pressed="false" />
</selector>
This works fine for API> 15, but only works for API 15 itemBackground
, but itemIconTint
it itemTextColor
statePressed
doesn't work. I do not know why. I also tried to set this programmatically in my activity
MainActivity
navigationView = (NavigationView) findViewById(R.id.nav_view);
int[][] states = new int[][] {
new int[] { android.R.attr.state_enabled}, // enabled
new int[] {-android.R.attr.state_enabled}, // disabled
new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_pressed} // pressed
};
int[] colors = new int[] {
Color.WHITE,
Color.RED,
Color.GREEN,
Color.BLUE
};
navigationView.setItemTextColor(new ColorStateList(states,colors));
Below are screenshots for API 15 and API 19. I tested this in API 16 as well and it worked fine.
source to share
No one has answered this question yet
Check out similar questions: