Home » Android » java – How to set background color of an Activity to white programmatically?

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

Posted by: admin March 11, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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

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

Answer:

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>

Answer:

?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

Answer:

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.

Answer:

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"

Answer:

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

Answer:

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

worked for me. thank you.

Answer:

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

Answer:

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