12

I want to keep some icons not tinted, and some tinted. Now I have:

app:itemIconTint="@color/menu_icons_selector"

It tints all icons.

I'm trying to make all icons not tinted

mNavigationView.setItemIconTintList(null);

and then

mNavigationView.getMenu().getItem(4).getIcon().setColorFilter(redColor, PorterDuff.Mode.SRC_ATOP);

to set tint only to 4th item, but this not working - all icons are now not tinted, and 4th also not tinted.

diesersamat
  • 667
  • 2
  • 7
  • 18
  • I just see you already use `setColorFilter` . In that case my answer probably won't help you much if it doesn't work (but you can leave it for future reference). Try removing the `app:itemIconTint` and `setItemIconTintList`. That should work I believe. Also make sure it is actually called. Mine runs in onPostCreate(). – miva2 Sep 07 '15 at 12:12

3 Answers3

12

This is working for me.... In this way you can tint individual Navigation MenuItem Icon Color Programmatically

navigation.getMenu().findItem(R.id.navItem1).getIcon().setColorFilter(Color.RED,PorterDuff.Mode.SRC_IN);
Soham
  • 4,397
  • 11
  • 43
  • 71
Raveesh G S
  • 524
  • 6
  • 7
2

You can "wrap" the icon as a drawable (res/drawable) and apply the applicable tint

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_menu_icon"
    android:tint="@color/pink"/>
0

If your icons are in an ImageView you can use setColorFilter.

Like in this example from the app I am working on, where the icon is tinted when it is selected in the navigationDrawer.

 ImageView iconView = (ImageView) view.findViewById(R.id.icon);

 iconView.setColorFilter(selected ?
     getResources().getColor(R.color.navdrawer_icon_selected_tint) :
     getResources().getColor(R.color.navdrawer_icon_tint));

You can apply setColorFilter also directly to the Drawable.

miva2
  • 2,111
  • 25
  • 34
  • Is it possible to get ImageViews of icons from NavigationView? – diesersamat Sep 10 '15 at 09:41
  • I don't think there is an `ImageView` inside NavigationView. You apply `setColorFilter` directly on the `Drawable` which you get from `getIcon()`. There is no need to get an `ImageView` since `ImageView` also applies it to the `Drawable`. – miva2 Sep 10 '15 at 09:54
  • As I said, I already setting ColorFilter to item, but it isn't working – diesersamat Sep 10 '15 at 14:28
  • I would try removing your first 2 lines. `app:itemIconTint` and `setItemIconTintList(null)` and only keep the third line you posted. If that doesn't work, I don't know :( – miva2 Sep 11 '15 at 07:59