I use this xml in two places, and i changed the color of one of the two programmatically, i found out that the other one's color also changed, why can this be?
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/blue" />
<corners android:radius="2dp" />
</shape>
Use
Drawable.mutate().Even though every time you load a
Drawablefrom resources you receive a new instance, for performance reasons, they all shareConstantState. ThisConstantStateusually holds all the properties that can be declared for aDrawable, its color in your example.Therefore if you modify the color of one of the
Drawablesthe change gets reflected in itsConstantStateand the change is visible for all the other instances which share the sameConstantState.As the documentation of
Drawable.mutate()states:You can check this great post by Romain Guy for more details.