Search code examples
androidandroid-layoutandroid-color

setBackgroundColor on a layout with a unique id also changes the color on other layouts


I have a RelativeLayout with a unique id and this configuration:

android:background="@color/popup_background"

The value of popup_background is #EDEDED and there are other color definitions that has this same value.

On this RelativeLayout with that unique id, I then sometimes set a new color like this:

myLayout.setBackgroundColor(getResources().getColor(R.color.popup_background_red));

The value of popup_background_red is #E3463D and this works just fine.

Now comes the problem. On some devices (e.g. Samsung Galaxy S3 and a old Google Nexus S and probably others), some of the other layouts in other views that has the background set to a color definition with the value #EDEDED is now displayed with the color #E3463D, although setBackgroundColor was never called on those layouts and they are completely different layouts with their own unique id's and living in totally different views. And to make things worse, it seems pretty random what layouts are effected (not always the same even on the same device) and only some de devices has this problem.

Does anybody have a clue to what is going on here? Can I do something to prevent this?

Thank you
Søren


Solution

  • Try using setBackgoundDrawable(new ColorDrawable(color))) rather than setBackground(color).

    Why you have to do this is explained here: https://stackoverflow.com/a/14875176/4747587