Android
ImageView
Tint
Programming
Tutorial

How can I set tint for an image view programmatically in Android?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Programmatically tinting an ImageView in Android is a common way to match icons to theme state, selection state, or brand colors. The main choice is whether to use the modern image tint APIs or the older color filter approach, depending on the drawable type and the Android versions you need to support.

Use imageTintList on Modern Android

For current Android code, the cleanest approach is to set a tint list on the ImageView. In Kotlin, that usually looks like this:

kotlin
1import android.content.res.ColorStateList
2import androidx.core.content.ContextCompat
3
4val tintColor = ContextCompat.getColor(this, R.color.teal_700)
5imageView.imageTintList = ColorStateList.valueOf(tintColor)

This changes how the image is rendered without replacing the drawable itself. It works especially well for vector drawables and monochrome icons.

If you need to change the tint later, just assign a new ColorStateList.

Use ImageViewCompat for Better Compatibility

When you want a compatibility-friendly API, use ImageViewCompat from AndroidX:

kotlin
1import android.content.res.ColorStateList
2import androidx.core.widget.ImageViewCompat
3import androidx.core.content.ContextCompat
4
5val tintColor = ContextCompat.getColor(this, R.color.purple_500)
6ImageViewCompat.setImageTintList(
7    imageView,
8    ColorStateList.valueOf(tintColor)
9)

This is a good default in apps that already depend on AndroidX, because it keeps the call style consistent across different API levels.

Older Approach With setColorFilter

Before image tint APIs were widely used, many apps tinted drawables with a color filter. That approach still works and is useful in some legacy codebases.

kotlin
1import androidx.core.content.ContextCompat
2import android.graphics.PorterDuff
3
4val tintColor = ContextCompat.getColor(this, R.color.red)
5imageView.setColorFilter(tintColor, PorterDuff.Mode.SRC_IN)

The result is similar for many simple icons, but the color filter approach is less expressive than a tint list and can behave differently with complex drawables.

Tint the Drawable Itself Carefully

If you apply tint directly to a drawable instance, make sure the drawable is mutable first. Otherwise, you can accidentally tint a shared constant-state drawable used elsewhere in the app.

kotlin
1import androidx.core.graphics.drawable.DrawableCompat
2
3val drawable = imageView.drawable.mutate()
4val wrapped = DrawableCompat.wrap(drawable)
5DrawableCompat.setTint(wrapped, tintColor)
6imageView.setImageDrawable(wrapped)

This matters because Android may reuse drawable state objects behind the scenes. Calling mutate() ensures your tint does not leak into other views that use the same resource.

When Tinting Works Best

Tinting is most predictable when the image is:

  • a vector drawable
  • a single-color icon
  • a shape drawable meant to inherit theme color

It is less predictable with full-color photos or illustrations because tinting applies a color transformation, not a selective recoloring workflow.

If the source image is multi-colored and you only want one part to change, the better fix is often to split the asset into layers or use a purpose-built drawable rather than force a tint onto the whole bitmap.

Remove or Reset a Tint

If the icon needs to return to its original color, clear the tint explicitly:

kotlin
imageView.imageTintList = null
imageView.clearColorFilter()

Only one of these may be necessary depending on which approach you used, but clearing both is a simple way to avoid stale rendering when migrating between tint methods.

Common Pitfalls

The first mistake is tinting a shared drawable without calling mutate(). That can unexpectedly recolor the same resource in other screens.

Another common issue is using a full-color bitmap and expecting the result to look like a themed icon. Tinting works best on assets designed for it, especially vectors and simple monochrome images.

Developers also sometimes mix imageTintList and setColorFilter without realizing both affect rendering. If debugging gets confusing, pick one approach and clear the other.

Finally, make sure the chosen color actually comes from the correct context or theme. Hard-coded values work for demos, but theme-aware colors usually make more sense in real applications.

Summary

  • Use imageTintList or ImageViewCompat.setImageTintList for modern Android code.
  • 'setColorFilter is still useful in older or legacy codebases.'
  • Call mutate() before tinting a drawable instance directly.
  • Tinting is most reliable for vectors and simple monochrome icons.
  • Clear old tint state when switching between different tinting approaches.

Course illustration
Course illustration

All Rights Reserved.