How to set tint for an image view programmatically in android?

user2968768 picture user2968768 · Nov 21, 2013 · Viewed 348.8k times · Source

Need to set tint for an image view... I am using it the following way:

imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);

But it doesn't change...

Answer

Hardik picture Hardik · Nov 21, 2013

You can change the tint, quite easily in code via:

imageView.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint

If you want color tint then

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);

For Vector Drawable

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);

UPDATE:
@ADev has newer solution in his answer here, but his solution requires newer support library - 25.4.0 or above.