Search code examples
androidandroid-layoutcolorsbackground

How to dynamically set background color in android app?


Been trying to figure this out for a year now. Got a temporary workaround a long time ago, but the issue has come back up as I prepare to add new features into the app I have developed.

Main goal: Have the user be able to choose virtually any color to use as background for the app.

Current iteration: I have 2 drawable images, one green, one blue. User is able to switch between the two, but only by:

if (bgColor) {
        getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_blue_background_simple));
    }
    else {
        getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_green_background));
    }
}

bgColor is the boolean if the user has changed the background from default green, to blue.

Now that I am trying to switch over to colors, and not drawables, I have tried:

LayoutInflater layInflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layInflate.inflate(R.layout.main_activity_layout, null);
    mainLayout = (RelativeLayout) view.findViewById(R.id.mainActivityRelativeLayoutId);
    mainLayout.setBackgroundResource(R.color.colorRed);

but it does not do anything!

So then I try to do it like it worked with the drawable images, but color (type int) is not type drawable! So this code does not work either.

The dilemma and question: I have a color picker settings/preference. The user can pick virtually any color from it and it saves it as a color of type int. It successfully works with text color:

textView.setTextColor(colorPickerColor);

colorPickerColor is variable of type int retrieved from the color picker preference

but when trying to use that to change background of layout or views, it does nothing. Even setting a constant/hardcoded color does not change the color (as shown in code above). The only way I've been able to change the background with a color is to user either drawable (working for me now, but extremely inefficient since I would have to have a separate drawable for each selectable color, potentially millions or billions), or hexadecimal code for color, but hard coded into the XML of the layout, since putting it in code does nothing (not useful at all since user won't be able to choose).

What is the proper way to change a layout's background color by specifying a color of type int programmatically/dynamically through code? Min API my app is supporting is 14

note: I've searched, but every result that came up was either not related, or did not work.


Solution

  • The ColorDrawable would be the solution for your case. Assume that the colorPickerColor is an integer color code to draw, the background may set with the following code:

    mainLayout.setBackgroundDrawable(new ColorDrawable(colorPickerColor));
    

    and if the colorPickerColor is a color resource id,

    mainLayout.setBackgroundDrawable(new ColorDrawable(getResources().getColor(colorPickerColor)));
    

    Kindly note that the setBackgroundDrawable method is deprecated, you may call setBackground instead.