Maxinne
Maxinne

Reputation: 1693

android imageview's tint set in xml overrides tint set on DrawableCompat programmatically

so another day, another problem! I've set an ImageView with a tint, as follows:

<ImageView
    android:id="@+id/image_money"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:padding="3dp"
    android:src="@drawable/ic_money_icon"
    android:tint="#c7c7c7"/>

And I need to revert or set the tintColor, so I used DrawableCompat as follows:

Drawable imagem = holder.view.getContext().getResources().getDrawable(R.drawable.ic_money_icon);
imagem = DrawableCompat.wrap(imagem);
imagem = imagem.mutate();
DrawableCompat.setTint(imagem,Color.parseColor("#43a085"));
holder.imageDebito.setImageDrawable(imagem);

I inspected the bitmap on imagem and it looks as it should, with the color that I've set, but when applied to the ImageView imageDebito it reverts to the tint set on the XML. If I take a different image that isn't used in the view, apply the tint, and then set it on the ImageView, it gets the same tint from the XML... I've tried setting setImageTintList(), but it isn't available on API level 17...

So, I need either to remove the tint property, or enforce the tint on the image over the ImageView's one from the xml.

Upvotes: 2

Views: 1690

Answers (1)

Chait
Chait

Reputation: 677

Use setColorFilter() method.

Drawable myIcon = getResources().getDrawable( R.drawable.button ); 
ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
myIcon.setColorFilter(filter);

Edit: just improved the formatting...

Upvotes: 1

Related Questions