Hi I am using android NavigationView. I want to give seperate text colour for some items in the NavigationView, not for all items. OR I want to change the text colour for a single item dynamically when I select an item in the NavigationView(Only for certain items). How can I do this?
Yes You can do it I have also done it.
First fetch the MenuItem to which you want to change the color
Menu m = navView.getMenu();
MenuItem menuItem = m.findItem(your_menu_id);
then apply spannable with your color
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(Color.your_color), 0, s.length(), 0);
menuItem.setTitle(s);
thats it..
Now Below code is for your 2nd solution changing text color dynamically on Menu click..
navView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
menuItem.setTitle(s);
return false;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With