How to set tint for an image view programmatically in android?
Most answers refer to using setColorFilter
which is not what was originally asked.
The user @Tad has his answer in the right direction but it only works on API 21+.
To set the tint on all Android versions, use the ImageViewCompat
:
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));
Note that yourTint
in this case must be a "color int". If you have a color resource like R.color.blue
, you need to load the color int first:
ContextCompat.getColor(context, R.color.blue);
UPDATE:
@ADev has newer solution in his answer here, but his solution requires newer support library - 25.4.0 or above.
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);