Issue
I have a TextView with a drawable as background. Using a StateListDrawable object I'm trying to set the background colour programmatically, but I'm running into unexpected behaviour: I set the colour in one object, and it changes in another. This should be impossible.
The relevant code:
GradientDrawable notPressed = (GradientDrawable) getResources().getDrawable(R.drawable.rectangle);
GradientDrawable isPressed = (GradientDrawable) getResources().getDrawable(R.drawable.rectangle);
isPressed.setColor(util.getColour(api, this));
StateListDrawable bg = new StateListDrawable();
// bg.addState(new int[] { android.R.attr.state_pressed }, isPressed);
bg.addState(StateSet.WILD_CARD, notPressed);
textView.setBackgroundDrawable(bg);
The drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp" android:color="@color/divider" />
<solid android:color="@color/background_button" />
</shape>
util.getColour
returns a colour resource based on the value of api.
The strange thing is that in the above code the colour of the isPressed
drawable is set, but after that this drawable is not used. Instead only the notPressed
drawable is added to the background of the textView.
But the background colour of the textView becomes the colour of the isPressed
drawable instead! This should be impossible, as they should be two different objects, even if they are created from the same drawable resource.
Solution
I think that when you get a resource you get the same reference to it. So notPressed
and isPressed
are the same object. I believe there's a clone operation somewhere...
Edit:
Yep, you have to call mutate()
on the drawable before you modify it. See Adding a color filter to a Drawable changes all Buttons using the same Drawable.
Answered By - dmon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.