How can I set the background color of an Activity to white programatically?
12 Answers
Add this single line in your activity, after setContentView()
call
getWindow().getDecorView().setBackgroundColor(Color.WHITE);

- 3,812
- 3
- 22
- 30
-
5agreed. this changes the colour of the window before the root layout is applied, the accepted answer changes the colour of the root element in the activity's layout – LairdPleng Oct 02 '13 at 01:46
-
2I think this one should be the correct one, if you just want to set the background colour of the activity. – shanwu Feb 20 '14 at 05:50
-
has my +1 as it changes the root window color – mparkes May 02 '16 at 23:35
-
1This is definitely the best answer – 1QuickQuestion Oct 14 '18 at 01:09
-
This is the easiest method for doing that. Actually better than accepted one ! – Dinith Oct 18 '19 at 09:30
-
1This should be the right answer. The accepted answer will cause overdraw. – Sotti Sep 21 '20 at 11:15
Get a handle to the root layout used, then set the background color on that. The root layout is whatever you called setContentView with.
setContentView(R.layout.main);
// Now get a handle to any View contained
// within the main layout you are using
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView();
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
-
When I do this, Eclipse flags it with "Should pass resolved color instead of resource id here: getResources().getColor(android.R.color.red)". – joriki Aug 06 '13 at 13:03
-
28Change last line to `root.setBackgroundColor(getResources().getColor(android.R.color.red));` – Luis Mendo Aug 13 '13 at 17:56
-
This answer works; but it is still not completely programmatic as per the questioner. I would suggest Arunkumar's answer below. – KVISH May 22 '15 at 03:29
-
I don't think this asnwer is correct. This is NOT setting the color to the activity, this will cause overdraw. The right answers is down below and should be like `window.decorView.setBackgroundColor(getResolvedColor(R.color.your_color))` – Sotti Sep 21 '20 at 11:14
I prefer coloring by theme
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>

- 1,510
- 20
- 16
-
23What's the difference between windowBackground and colorBackground? – AlikElzin-kilaka Jul 30 '13 at 20:36
-
1Just set the windowBackground and it works fine. What's the use of colorBackground? – codezjx Jul 14 '15 at 02:33
-
9@AlikElzin-kilaka: the difference is that when the app starts the `android:windowBackground` is visible first, for a brief moment, and then the layout background color takes over. So if you use two different colors, it will flicker on the screen. – GoTo Feb 05 '16 at 20:43
-
3`windowBackground` affects only window background, but `colorBackground` affects all views as well. https://stackoverflow.com/questions/26266221/difference-between-androidwindowbackground-and-androidcolorbackground – fdermishin Jan 22 '18 at 11:38
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:id="@+id/myScreen"
</LinearLayout>
In other words, "android:background" is the tag in the XML you want to change.
If you need to dynamically update the background value, see the following:

- 26,901
- 13
- 88
- 119
-
Mm good point. Well regardless, the link I gave answers that question pretty handily. – I82Much Jan 21 '11 at 18:35
-
i dont think you give me the right values for the color!! i got it would with #FFFFFF – SJS Jan 21 '11 at 20:48
-
This is a good answer for those of us that want to do it in xml and got here via a google search. – Kacy Jul 25 '15 at 18:34
In your onCreate()
method:
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));
Also you need to add to values folder a new XML file called color.xml
and Assign there a new color property:
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_activity_background_color">#000000</color>
</resources>
Note that you can name the color.xml
any name you want but you refer to it by code as R.color.yourId
.
EDIT
Because getResources().getColor()
is deprecated, use getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
instead.

- 1,238
- 2
- 18
- 45
-
This *almost* worked for me as posted, but after burning an embarrassing amount of time, I discovered that in my context I needed: `getWindow().getDecorView().findViewById( android.R.id.content ).setBackgroundColor( backgroundColor );` – BuvinJ Nov 15 '22 at 00:59
You can use this to call predefined android colours:
element.setBackgroundColor(android.R.color.red);
If you want to use one of your own custom colours, you can add your custom colour to strings.xml and then use the below to call it.
element.setBackgroundColor(R.color.mycolour);
However if you want to set the colour in your layout.xml you can modify and add the below to any element that accepts it.
android:background="#FFFFFF"

- 3,198
- 1
- 23
- 34
-
If I use the first technique I get a warning that it should actually be accessed like this: getResources().getColor(android.R.color.black); – Oct 19 '13 at 14:06
Button btn;
View root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
root =findViewById(R.id.activity_main).getRootView();
root.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
});
}

- 1,046
- 9
- 12
To get the root view defined in your xml file, without action bar, you can use this:
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
So, to change color to white:
root.setBackgroundResource(Color.WHITE);

- 1,495
- 14
- 15
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);
worked for me. thank you.

- 37
- 1
-
This answer is in the Low Quality Posts review queue because it's just code with no explanation. Please improve your answer by explaining what your code does and how it answers the question. Please read [this advice on answering programming questions helpfully](http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx). – Adi Inbar Jun 12 '14 at 18:48
-
3Kinda funny that this clearly sets the background to blue and not white as requested. – Konrad Lindenbach Jun 12 '14 at 18:58
final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);

- 26,253
- 19
- 107
- 134
for activity
findViewById(android.R.id.content).setBackgroundColor(color)

- 3,602
- 9
- 39
- 52

- 734
- 8
- 14
The best method right now is of course
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
Please be aware though, if you have anything set as the background color in Designer, it will overwrite anything you try to set in your code.

- 33
- 5