Change text color of MenuItem in Navigation Drawer


Navigation drawer style is my worst nightmare. In my navigation drawer I have different items with different text colors. In this post I will show you how we can change MenuItems text color programmatically.

For doing this we need two methods:

  1. Set text color for menu item:
  2.   private void setTextColorForMenuItem(MenuItem menuItem, @ColorRes int color) {
        SpannableString spanString = new SpannableString(menuItem.getTitle().toString());
        spanString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, color)), 0, spanString.length(), 0);
  3. Reset all menu items text color:
  private void resetAllMenuItemsTextColor(NavigationView navigationView) {
    for (int i = 0; i < navigationView.getMenu().size(); i++)
      setTextColorForMenuItem(navigationView.getMenu().getItem(i), R.color.textPrimary);

We are almost done, we should just set text color for each menu item in onNavigationItemSelected method like below:

  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    setTextColorForMenuItem(item, R.color.colorPrimary);

    switch (item.getItemId()) {
        setTextColorForMenuItem(item, R.color.nav_search);
        // do other stuff
        setTextColorForMenuItem(item, R.color.nav_recommendation);
        // do other stuff

That’s it ๐Ÿ˜‰


One Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.