I've recently updated my phone to Android Marshmallow and ran my existing app on it, but noticed a difference in color behavior: When applying changes to the background of a view (drawable), all views that share the same background (reference) will also the same changes applied. While previously, this was not the case.
In this example, I have a two views with the same background color, and I want to change the alpha level of one of both views.
First we define the views in the layout:
LinearLayout test1 = (LinearLayout) findViewById(R.id.test1);
LinearLayout test2 = (LinearLayout) findViewById(R.id.test2);
Most likely the class of each view's background and constantstate are the same object. It seems as if the two color resources have been "merged" somewhere -- meaning they have shared ConstantState. Maybe in the Resources class' caching? I would've expected them to stay separate since they're different resources (albeit with the same color value), but apparently not.
– Snild Dolkow
The ColorDrawable's state stores alpha, so any changes to one will change the others. To prevent this, you can first call mutate() on the drawable, separating the two drawables (by making a copy of the state).
In the example, this would result in using
test1.getBackground().mutate().setAlpha(80); instead of directly applying the alpha.