M
M
Maybe_V2016-03-31 00:30:09
Android
Maybe_V, 2016-03-31 00:30:09

How to correctly change the color of a component?

Faced an unexpected problem with the display of colors!
I need to set colors for a round button depending on some factors. I change its color like this:

switch (i){
            case 0:{
                StateListDrawable stateListDrawable = (StateListDrawable) typeIndicator.getBackground();
                stateListDrawable.setColorFilter(context.getResources().getColor(R.color.type_task_indicator_first), PorterDuff.Mode.MULTIPLY);
                break;
            }
            case 1:{
                StateListDrawable stateListDrawable = (StateListDrawable) typeIndicator.getBackground();
                stateListDrawable.setColorFilter(context.getResources().getColor(R.color.type_task_indicator_second), PorterDuff.Mode.MULTIPLY);
                break;
            }
            case 2:{
                StateListDrawable stateListDrawable = (StateListDrawable) typeIndicator.getBackground();
                stateListDrawable.setColorFilter(context.getResources().getColor(R.color.type_task_indicator_three), PorterDuff.Mode.MULTIPLY);
                break;
            }
        }

Should be displayed red, yellow and green in accordance!!!

BUT it doesn't look like this at all:

4680aa9e3f174c84b0725f5f24179a45.jpgI took these colors:
<color name="type_task_indicator_three">#4CAF50</color>
    <color name="type_task_indicator_second">#f1c40f</color>
    <color name="type_task_indicator_first">#e74c3c</color>

What could be the problem ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-03-31
@prokopov-vi

1) Why not make different drawables for different backgrounds?
2) In my opinion, the problem is PorterDuff.Mode.MULTIPLY, and how it interacts with the real background color (by the way, what color without filters?). Well, I would try PorterDuff.Mode. DST_ATOP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question