Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give custom text colour for a single menu item in a NavigationView?

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?

like image 425
Sudheesh Mohan Avatar asked Jun 26 '15 11:06

Sudheesh Mohan


1 Answers

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;
        }
    });
like image 133
Moinkhan Avatar answered Nov 15 '22 15:11

Moinkhan