3
<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/colorRed"
    android:background="@drawable/ic_delete"/>

As above I added an Android VectorAsset for ImageView background.
And I can change color of that Vector Asset from red to blue by xml like below.

android:backgroundTint="@color/colorBlue"

but I want to change its color programmatically.

Cœur
  • 37,241
  • 25
  • 195
  • 267
thinuwan
  • 631
  • 2
  • 8
  • 20

5 Answers5

6

Instead of using ImageView you can use AppCompatImageView, Because setBackgroundTintList is supported from API level 21, If you use AppCompatImageView you can change the tint color using setSupportBackgroundTintList.

so change you ImageView like this,

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/colorRed"
    android:background="@drawable/ic_delete"/>

So that you can call setSupportBackgroundTintList to set the tint color like this,

imageView.setSupportBackgroundTintList(ContextCompat.getColorStateList(this, R.color.colorBlue));
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
2

programmatically you can achieve it like this:

img.setColorFilter(getResources().getColor(R.color.white));
Cœur
  • 37,241
  • 25
  • 195
  • 267
Marium Jawed
  • 391
  • 4
  • 9
0

First of all background and src are different properties for a ImageView. Are you trying to set an Image to ImageView? First of all You have to use the property: android:src="drawable"

If you are using a vector asset you will need to use the property:app:srcCompat="drawable"

To understand the background, backgroundTint properties have a read at this: What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

AND to finally answer your question to background tint programmatically if that is what you really want, then here is a link How to add button tint programmatically

Hope it helps!

theAndDev
  • 662
  • 2
  • 11
  • 33
0

This question is related, but answers for buttons.

Original answer by user A.A


You should use setBackgroundTintList(ColorStateList list)

Follow this link to know how create a Color State List Resource.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="#your_color_here" />
</selector>

then load it using

setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));

where contextInstance is an instance of a Context

using AppCompat

btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));
LeoColman
  • 6,950
  • 7
  • 34
  • 63
0
    imageView.apply {
        setColorFilter(ContextCompat.getColor(context, tintColor), android.graphics.PorterDuff.Mode.MULTIPLY)
        backgroundTintList = ContextCompat.getColorStateList(context, bgTintColor)
    }
nAkhmedov
  • 3,522
  • 4
  • 37
  • 72