I\'m trying to add a night theme for my app and I\'ve wasted nearly three hours just trying to make the text and icons in my navigation drawer turn white along with the dark
In my case, I needed to change the color of just one menu item - "Logout". I had to run a recursion and changed the title color:
NavigationView nvDrawer;
Menu menu = nvDrawer.getMenu();
for (int i = 0; i < menu.size(); i ++){
MenuItem menuItem = menu.getItem(i);
if (menuItem.getTitle().equals("Logout")){
SpannableString spanString = new SpannableString(menuItem.getTitle().toString());
spanString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), 0, spanString.length(), 0);
menuItem.setTitle(spanString);
}
}
I did this in the Activity's onCreate method.
In Future if anyone comes here using, Navigation Drawer Activity (provided by Studio in Activity Prompt window)
The answer is -
Use this before OnCreate() in MainActivity
int[][] state = new int[][] {
new int[] {android.R.attr.state_checked}, // checked
new int[] {-android.R.attr.state_checked}
};
int[] color = new int[] {
Color.rgb(255,46,84),
(Color.BLACK)
};
ColorStateList csl = new ColorStateList(state, color);
int[][] state2 = new int[][] {
new int[] {android.R.attr.state_checked}, // checked
new int[] {-android.R.attr.state_checked}
};
int[] color2 = new int[] {
Color.rgb(255,46,84),
(Color.GRAY)
};
ColorStateList csl2 = new ColorStateList(state2, color2);
and use this in onNavigationItemSelected() in MainActivity (you dont need to Write this function if you use Navigation Drawer activity, it will be added in MainActivity).
NavigationView nav = (NavigationView) findViewById(R.id.nav_view);
nav.setItemTextColor(csl);
nav.setItemIconTintList(csl2);
nav.setItemBackgroundResource(R.color.white);
Tip - add this code before If else Condition in onNavigationItemSelected()
To change the individual text color of a single item, use a SpannableString like below:
SpannableString spannableString = new SpannableString("Menu item"));
spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0, spannableString.length(), 0);
menu.findItem(R.id.nav_item).setTitle(spannableString);
You can also define a custom theme that is derived from your base theme:
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/nav_view"
app:headerLayout="@layout/nav_view_header"
app:menu="@layout/nav_view_menu"
app:theme="@style/MyTheme.NavMenu" />
and then in your styles.xml file:
<style name="MyTheme.NavMenu" parent="MyTheme.Base">
<item name="android:textColorPrimary">@color/yourcolor</item>
</style>
you can also apply more attributes to the custom theme.
itemIconTint, if u want to change icon color
android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor="@color/colorPrimary"
app:itemIconTint="@color/colorPrimary"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
More options:
you can also change the group title by overriding "textColorSecondary"
In your styles.xml
<style name="AppTheme.NavigationView">
<item name="android:textColorSecondary">#FFFFFF</item>
</style>
In your layout
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_menu_drawer_drawer"
android:theme="@style/AppTheme.NavigationView"
app:itemIconTint="@color/colorPrimary"
app:itemTextColor="@color/white"/>