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


Question

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

1
123
5/19/2016 6:12:18 PM

Accepted Answer

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));
134
10/25/2016 7:48:27 AM

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon