Search code examples
androidandroid-layoutandroid-menuandroid-navigationview

Can I make navigation menu item text to be different than other items?


I'm using NavigationView and menu on my app. I want one of the menu items to to be in different color (Icon and text).

I can make the icon show other color using setItemIconTintList(null) and using icons in the required colors. Is there a way to make a single menu item text color to be different?


Solution

  • Inspired by DevTest's answer, I've eventually used SpannableString for setting a text with a specific color. I've created a util method for it:

    public static CharSequence getSpannableColorString (String text, int color) {
        SpannableString spanString = new SpannableString(text);
        spanString.setSpan(new ForegroundColorSpan(color),0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
        return spanString;
    }
    

    Then used it for setting my MenuItem's title:

    MenuItem menuItem = navigationView.getMenu().findItem(R.id.myItem);
    menuItem.setTitle(getSpannableColorString(getString(R.string.itemText), getColor(R.color.itemColor)));