Search code examples
android-studionavigation-drawertextcolor

Change text color of a portion of navigation drawer item


In Android Studio, I want to change the color of a portion of a item in the Navigation Drawer. In the image attached Navigation Drawer, I want the word "PRO" to appear red.


Solution

  • Use a spannable string:

    MenuItem item = (MenuItem) navigation.getMenu().findItem(R.id.miItem);
    String str = item.getTitle().toString();
    SpannableString span = new SpannableString(str);
    span.setSpan(new ForegroundColorSpan(Color.RED), str.length() - 3, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    item.setTitle(span);
    

    replace
    navigation with your NavigationView variable
    miItem with the corresponding MenuItem's id