I'm attempting to colorize a graphic using setColorFilter
. The following code seems to work fine on lollipop, but it seems to have no effect on kitkat, the icon is rendered in it's original colors:
Drawable icon = ContextCompat.getDrawable(context, R.drawable.ic_chat_button).mutate();
icon.setColorFilter(context.getResources().getColor(R.color.control_tint_color), PorterDuff.Mode.SRC_ATOP);
icon.invalidateSelf();
The mutate
and invalidateSelf
calls don't seem to have any effect on the problem here, just leaving them in as an example of part of what's been tried to figure out what's going on.
FWIW, I'm using the drawable as part of a LayerDrawable
in a StateListDrawable
that gets used as either the background for a button or as the drawable for an ImageView
The results are consistent (ie., wrong on kitkat) either way. I've also tried putting the icon drawable directly into the StateListDrawable
again with no change in behavior. In all cases, it works fine on lollipop, but doesn't work on kitkat.
As an experiment, I took the tinted Drawable
out of the StateListDrawable
but not the LayerDrawable
and it works as expected. Apparently there's something flawed in KitKat's implementation of StateListDrawable
that prevents it from working, that has been remedied in later versions.
Ultimately, it seems like the problem is that KitKat doesn't support using a
ColorFilter
(or implicitly an alpha) on aDrawable
that will in turn be in aStateListDrawable
. My solution was to use the same to code to construct the complexDrawable
and then render that into a simpleBitMapDrawable
:Rather than attach the coloring to something like "disabled" state (as in the accepted answer) I found the answer to be simpler by focusing on recoloring and let my usage leverage how to include the now-tinted image in the
StateListDrawable
. (And FYI, I've tried to translate from the Xamarin C# I'm using, but the below code may not complie correctly as Java)Finally, in all fairness to the accepted answer, I'm rather foreign to Android development and can thank him for showing the pieces I needed before then simplifying them.