Reputation: 1165
I'm facing a strange behavior when using ColorFilter with drawables on Lollipop devices.
I'm trying to change the color of some drawables for the whole application. I used to do that by applying a ColorFilter on it, and then as drawables are sharing their ConstantState, all instances were modified. This is working great on pre lollipop device. However, when running on a lollipop device this seems to be broken and modifications could only be applied to a single instance. Here is a simple example that works fine on pre lollipop and not after that:
int color = Color.BLUE;
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
addContentView(iv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
On pre lollipop device image is blue, on post lollipop devices, image is unchanged.
And this example is working on both, but is only applied on this instance of that drawable and not when reusing it elsewhere:
int color = Color.BLUE;
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
ImageView iv = new ImageView(this);
iv.setImageDrawable(drawable);
addContentView(iv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
As far as I understand, this could be linked to Tint effect on Lollipop applying its own ColorFilter but ImageView here isn't clickable. So I tried applying setTint method on my drawable without success.
So my question is what happened on drawables's constantState in Lollipop release, how can I get my snipnet to work again ?
Thanks.
Upvotes: 0
Views: 491
Reputation: 501
For ImageView you can change color using below code:
imgView.setColorFilter(iconColor, Mode.MULTIPLY);
It works fine for all versions.
Upvotes: 1