How can I set background color of an activity before setContentView
.
I have an activity which takes lot of time to load, I need to keep background white until activity finishes loading.

- 97,681
- 90
- 411
- 885

- 17,993
- 23
- 107
- 210
-
1Try to add spinner in that activity and make the background transparent – Rakshit Nawani May 14 '15 at 05:58
-
Make `rootView` Background White till your Loading and then make it different color after loading done – M D May 14 '15 at 05:59
-
@MD I tried this `getWindow().getDecorView().findViewById(android.R.id.content).setBackgroundColor(0xffffffff);` but it's not working. – AVEbrahimi May 14 '15 at 06:05
-
@AVEbrahimi What is this id `android.R.id.content` ? – M D May 14 '15 at 06:05
-
Related: [How to set background color of Activity to white programmatically?](http://stackoverflow.com/q/4761686) – jww May 14 '15 at 10:47
3 Answers
You can do one of the following:
setContentView
can be called multiple times in activity lifecycle, So you can simply set another layout file which just shows the background as you require, and once you are done loading, call setContentView
again to load the actual layout!
OR
You can simply manage all this in one layout file, by giving white background color to your root view than show/hide desired section as required!

- 4,596
- 2
- 22
- 38
In your oncreate add the following
Spinner = (ProgressBar) findViewById(R.id.LoginPageProgressBar);
Spinner.setVisibility(View.GONE);
and when you click the button add the following before using intent
Spinner.setVisibility(View.VISIBLE);

- 2,604
- 14
- 27
From Documentation
setContentView(int layoutResID)
Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
So , it is not possible to give some background color before setContentView()
But try to
Avoid too much code in
onCreate()
ofActivity
.Use
AsyncTask
to load Data & show aprogressbar
to the userOr you can implement some
splash screen
before loading the mainActivity
.

- 7,397
- 5
- 44
- 59