How to set background color of an Activity to white programmatically?

JavaAndroidAndroid ActivityColors

Java Problem Overview


How can I set the background color of an Activity to white programatically?

Java Solutions


Solution 1 - Java

Add this single line in your activity, after setContentView() call

getWindow().getDecorView().setBackgroundColor(Color.WHITE);

Solution 2 - Java

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));

Solution 3 - Java

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>

Solution 4 - Java

?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:

Exercise: Change background color, by SeekBar

Solution 5 - Java

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.

Solution 6 - Java

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"

Solution 7 - Java

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);

Solution 8 - Java

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"));
        }
    });
}

Solution 9 - Java

View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);

worked for me. thank you.

Solution 10 - Java

final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);

Solution 11 - Java

for activity

findViewById(android.R.id.content).setBackgroundColor(color)

Solution 12 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSJSView Question on Stackoverflow
Solution 1 - JavaArunkumarView Answer on Stackoverflow
Solution 2 - JavaHamyView Answer on Stackoverflow
Solution 3 - JavaDavidView Answer on Stackoverflow
Solution 4 - JavaI82MuchView Answer on Stackoverflow
Solution 5 - JavaGodView Answer on Stackoverflow
Solution 6 - JavaLuke AldertonView Answer on Stackoverflow
Solution 7 - JavaKitKatView Answer on Stackoverflow
Solution 8 - JavaSaleem KalroView Answer on Stackoverflow
Solution 9 - JavaHemal AdaniView Answer on Stackoverflow
Solution 10 - JavardsView Answer on Stackoverflow
Solution 11 - JavaSheikh Zakir AhmadView Answer on Stackoverflow
Solution 12 - JavaFatherlyNickView Answer on Stackoverflow