Search code examples
androidandroid-layoutandroid-themeandroid-activityrecord

android changing background color too slow


I want my program to have in every activity the same background color. So I put this line of code in every main layout of each layout.xml file.

android:background="@color/background"

So far so good, it works and looks nice. However, I recently started to notice when I call a new activity it first has a white background which changes immediately to the background color. But there is always this white color in my eyes for a very short time, which is really annoying.

Does anyone know how to avoid this problem? I want my activities to normally with their new background colors.

Edit : Well every activity I use has the same background. That's how I start new activities :

startActivity( new Intent( getActivity(), ActivityAlbumContent.class ) );

So far I have only this one and the MainActivity but it's also on application start that the MainActivity has first a white background. I can now only assume that the Activities I will create later will have the same problem.

This is inside my styles.xml.

<style name="CustomTheme" parent="android:Theme">
    <item name="android:windowBackground">@color/background</item>
</style>

The windowBackground attribute does not exist somehow. I set this theme to my application in the manifest file but then it crashes.

06-30 20:43:49.048: E/AndroidRuntime(15411): FATAL EXCEPTION: main
06-30 20:43:49.048: E/AndroidRuntime(15411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.davlog.mplayer/com.davlog.mplayer.MainActivity}: java.lang.NullPointerException
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.app.ActivityThread.access$700(ActivityThread.java:154)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.os.Looper.loop(Looper.java:137)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.app.ActivityThread.main(ActivityThread.java:5306)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at java.lang.reflect.Method.invokeNative(Native Method)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at java.lang.reflect.Method.invoke(Method.java:511)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at dalvik.system.NativeStart.main(Native Method)
06-30 20:43:49.048: E/AndroidRuntime(15411): Caused by: java.lang.NullPointerException
06-30 20:43:49.048: E/AndroidRuntime(15411):    at com.davlog.mplayer.MainActivity.onCreate(MainActivity.java:29)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.app.Activity.performCreate(Activity.java:5255)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-30 20:43:49.048: E/AndroidRuntime(15411):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
06-30 20:43:49.048: E/AndroidRuntime(15411):    ... 11 more

Solution

  • You can use the android:windowBackground property in a custom theme to set the background color of an Activity, including when the Activity is loading.

    For example, define this style:

    <style name="Theme.MyApp" parent="android:Theme">
        <item name="android:windowBackground">@color/my_app_color</item>
    </style>
    

    Then in your manifest you select the theme like so:

    <application
            android:theme="@style/Theme.MyApp">
    
        <!- ... ->
    
    </application>
    

    This eliminates the need to set android:background for each of your layouts, and will display the appropriate color while Activities load.

    Note that if you are supporting any devices above API 14, you probably want to have a values-14/styles.xml copy of this theme that inherits from a Holo theme. If you are only supporting devices above 14, then you only need one theme that inherits from a Holo theme.