Answer the question
In order to leave comments, you need to log in
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;
}
}
<color name="type_task_indicator_three">#4CAF50</color>
<color name="type_task_indicator_second">#f1c40f</color>
<color name="type_task_indicator_first">#e74c3c</color>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question