13

I am trying to flash different colours onto the screen at regular intervals (a few times per second).

To change the colours, I use Drawable.setColorFilter(int color, Mode mode) on the background of my main view:

  • myView.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC);

For debugging purposes, I added another view that I change using View.setBackgroundColor(int color).

The problem is that the setColorFilter() calls are working on Lollipop, but are broken on previous versions (specifically Nexus 7 v4.4.4, Galaxy Nexus v4.2.1).


I call the colour changing code inside a Runnable that is triggered at regular intervals by a Handler.

The handler is being called on all platforms (I can see the background changes due to the debugging setBackgroundColor() calls).

Below is the colour cycling code:

Handler mHandler;
RunnableOnTick thisRunnable;
View vDebug;
View vBroken;

class RunnableOnTick implements Runnable
{
    int backgroundColor;

    @Override
    public void run()
    {
        color = random.nextInt(2);

        switch (color)
        {
            case 0:
            {
                backgroundColor = Color.RED;
                break;
            }
            case 1:
            {
                backgroundColor = Color.GREEN;
                break;
            }
        }

        // this works on all platforms
        vDebug.setBackgroundColor(backgroundColor);

        // this works only on Lollipop
        vBroken.getBackground().setColorFilter(backgroundColor, PorterDuff.Mode.SRC);
        vBroken.invalidate();

        mHandler.postDelayed(thisRunnable, 100);
    }
}

I have tried different PorterDuff.Mode values - still can't get it working on Android 4.

What is different between Android v4 and v5 that would change the way setColorFilter() works?

5
  • Which Drawable you're using exactly? It might depend on particular implementation. Aug 14, 2015 at 15:48
  • @Dmitry If I remember correctly I was just using the stock green android launcher icon as this was a proof-of-concept I threw together. In the end I used a different method and haven't looked at that app in months. Aug 14, 2015 at 15:52
  • Then it was BitmapDrawable. Well, if you don't have the app anymore, then it's hard to find the problem now. I can certainly say that color filter was working in KitKat (you can simply try to use tint attribute of ImageView - it is backed by ColorFilter) Aug 14, 2015 at 16:16
  • @Dmitry I'll see what I can pull out the repo. I am always interested to know if the bug lies with me, of a very well tested framework. Will also test the supplied answer code and comment. Thanks for the input. Aug 14, 2015 at 19:01
  • @Richard try below method
    – Hardik
    Aug 15, 2015 at 10:14

7 Answers 7

6
+50

Ultimately, it seems like the problem is that KitKat doesn't support using a ColorFilter (or implicitly an alpha) on a Drawable that will in turn be in a StateListDrawable. My solution was to use the same to code to construct the complex Drawable and then render that into a simple BitMapDrawable:

 static Drawable createDrawable(Context context, int color, boolean disabled) {
OvalShape oShape = new OvalShape();
ShapeDrawable background = new ShapeDrawable(oShape);
background.getPaint().setColor(color);

ShapeDrawable shader = new ShapeDrawable(oShape);
shader.setShaderFactory(new ShapeDrawable.ShaderFactory() {
    @Override
    public Shader resize(int width, int height) {
        return new LinearGradient(0, 0, 0, height,
                new int[]{
                        Color.WHITE,
                        Color.GRAY,
                        Color.DKGRAY,
                        Color.BLACK
                }, null, Shader.TileMode.REPEAT);
    }
});

Drawable icon = ContextCompat.getDrawable(context, R.drawable.ic_chat_button).mutate();
icon.setColorFilter(context.getResources().getColor(R.color.control_tint_color), PorterDuff.Mode.SRC_IN);

Drawable layer = new LayerDrawable(new Drawable[]{ shader, background, icon });
layer.setAlpha(disabled ? 128 : 255);

// Note that on KitKat, setting a ColorFilter on a Drawable contained in a StateListDrawable
//  apparently doesn't work, although it does on later versions, so we have to render the colored
//  bitmap into a BitmapDrawable and then put that into the StateListDrawable
Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

layer.setBounds(0, 0, layer.getIntrinsicWidth(), layer.getIntrinsicHeight());
layer.draw(canvas);

return new BitmapDrawable(context.getResources(), bitmap);
}
4
  • 1
    Do you have any reference for that statement about KitKat not supporting it? Aug 17, 2015 at 11:34
  • No, i don't have any such reference but above solution is working...mark my answer up..if this helps.. Aug 17, 2015 at 11:45
  • Solve my problem, not only kitkat but I found that it wasn't working for API < 21, but with this workaround does. won the bounty. Aug 18, 2015 at 14:12
  • This is the issue persisting for me as well in 4.4 and 4.3.1 , both Samsung devices. Unfortunately this fix doesn't work for me.
    – sud007
    Jul 18, 2016 at 11:56
3

There's an issue in AppCompat with compound Drawable below API 21 that me thinks is related: https://code.google.com/p/android/issues/detail?id=191111

The simple solution is not using drawables from XML but create them in code and then apply setColorFilter. That's why @Hardeep solution worked.

Fun trivia: In my case setColorFilter on XML-created TextView drawableLeft worked fine, but only when invoked via click handler / delayed. When invoked in onCreate / onResume etc nothing happened.

1
  • lol thanks to your fun trivia, spent almost 3 hours looking for solution, posting a delayed runnable did the trick at OnClick
    – Flux
    Oct 6, 2016 at 5:18
3

I had the same issue on pre-lollipop, I solved replacing:

vBroken.getBackground().setColorFilter(backgroundColor, PorterDuff.Mode.SRC);

with:

    Drawable d = vBroken.getBackground();
    d.setColorFilter(backgroundColor, PorterDuff.Mode.MULTIPLY);
    vBroken.setBackground(d);
1
  • perfect solution, this must be selected as accepted one, thank you @mengoni Oct 16, 2019 at 15:19
1

For me, a ColorFilter would not be applied to a color item in a StateListDrawable.

Creating a Drawable to represent that color and using that instead in the StateListDrawable saw setColorFilter working on pre-Lollipop devices I tested.

As a side note, I made a pure white Drawable so that tinting color would be applied at full opacity.

1
  • This is the right answer. Color items can't be changed by setColorFilter and should be changed to any drawable, e.g. 1px x 1px drawables items Jul 11, 2019 at 9:24
0

Have you tried directly like this?!

vBroken.getBackground().setColorFilter(Color.argb(255, 255, 255, 255),PorterDuff.Mode.MULTIPLY));
0
0

In order to paint drawable for as many different states as you want on all android versions, you can use this helper class:

public class MagicDrawable extends StateListDrawable {

private Map<Integer, Integer> stateColorsMap;
private int defaultColor;

public static MagicDrawable create(Drawable drawable, int defaultColor, int pressedColor, int selectedColor){
    Map<Integer, Integer> map = new HashMap();
    map.put(android.R.attr.state_selected, selectedColor);
    map.put(android.R.attr.state_pressed, pressedColor);

    MagicDrawable stateDrawable = new MagicDrawable(drawable, defaultColor, map);
    return stateDrawable;
}

public static MagicDrawable createSelected(Drawable drawable, int defaultColor, int selectedColor){
    Map<Integer, Integer> map = new HashMap();
    map.put(android.R.attr.state_selected, selectedColor);

    MagicDrawable stateDrawable = new MagicDrawable(drawable, defaultColor, map);
    return stateDrawable;
}

public static MagicDrawable createPressed(Drawable drawable, int defaultColor, int pressedColor){
    Map<Integer, Integer> map = new HashMap();
    map.put(android.R.attr.state_pressed, pressedColor);

    MagicDrawable stateDrawable = new MagicDrawable(drawable, defaultColor, map);
    return stateDrawable;
}

/**
 *  Create state list drawable programmatically - just pass drawable, default color of drawable, and Map of (state , color)
 * @param drawable resourse for icons
 * @param defaultColor color for normal state
 * @param stateColorsMap map of for ex. android.R.attr.state_pressed and ColorManager.someColor
 *
 *                        Map<Integer, Integer> map = new HashMap<>();
map.put(android.R.attr.state_pressed, ColorManager.rose);
 */
public MagicDrawable(Drawable drawable, int defaultColor, Map<Integer, Integer> stateColorsMap) {
    super();
    this.stateColorsMap = stateColorsMap;
    this.defaultColor = defaultColor;

    drawable.setColorFilter(defaultColor, PorterDuff.Mode.SRC_IN);

    if (stateColorsMap != null) {
        for (int state : stateColorsMap.keySet()) {
            addState(new int[]{state}, drawable);
        }
    }

    addState(new int[] {}, drawable);
}

@Override
protected boolean onStateChange(int[] states) {

    if (stateColorsMap == null) {
        super.setColorFilter(defaultColor, PorterDuff.Mode.SRC_IN);
        return super.onStateChange(states);
    }

    boolean colorSet = false;

    for (int state : states) {

        for (int st : stateColorsMap.keySet()){

            if (state == st) {
                super.setColorFilter(stateColorsMap.get(st), PorterDuff.Mode.SRC_IN);
                colorSet = true;
                break;
            }
        }
    }

    if (!colorSet) {
        super.setColorFilter(defaultColor, PorterDuff.Mode.SRC_IN);
    }

    return super.onStateChange(states);
}

@Override
public boolean isStateful() {
    return true;
}

}

-4
/**
     * Tint / Colorise the Supplied {@code drawable} into another color of supplied {@code colorResId}
     * @param context
     * @param drawable
     * @param colorResId
     * @return
     */
    public Drawable tintThisDrawable(Context context ,Drawable drawable,@ColorRes int colorResId)
    {
        Resources res = context.getResources();
        int color = res.getColor(colorResId);
        if (drawable != null) {
            drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        }
        return drawable;
    }

I remember this function working somewhere in my Project. Please test this yourself.

6
  • That's almost exactly what is written in his answer Aug 14, 2015 at 16:11
  • Well! now I see it ,It deals with drawables and not color. If He can use Drawable to set Background this would work.
    – Y2K
    Aug 14, 2015 at 16:15
  • This is exactly what he is doing. Please, read the question before answering. Aug 14, 2015 at 16:17
  • 1
    Even if it works - it still does not answer the question. Aug 14, 2015 at 16:37
  • 1
    The one difference I can see is that I was trying with View.getBackground().setColorFilter(). Perhaps there is a difference between that call, and what you are demonstrating? I will try and look into it again in the next week or so. Aug 14, 2015 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.