Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app loading screen background image takes too long to load

Tags:

android

I am developing an android app. In my case, when the app starts it displays a temporary screen while AsyncTask fetches data in the background. This temporary screen contain only a background image (20 KB). But in this background takes a while to load. As a result of this when I start my app I only see a white screen, after a while it shows the background image. Any ideas on how to get rid of this problem and show the background image from the beginning Here is the xml file of the start screen

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/start"
    android:orientation="vertical" >

</LinearLayout>

Thanks

like image 747
Alexander Avatar asked Jan 28 '13 22:01

Alexander


1 Answers

I had the same problem. This awesome person has the explanation.

TL;DR (only in case the link goes bad, I would go read it though) :"the purpose of preview windows is to give the user immediate feedback that the app launched, but it also gives your app time to initialize itself. When your app is ready to run, the system removes the window and displays your app’s windows and views." You can set this in themes. Minimally, my setup:

Theme in /res/values/styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowBackground">@drawable/app_bg</item>
    </style>
    <!-- Base theme for any other activity other than your starting one. -->
    <!-- If only one activity, just set the splash bg as its bg -->
    <style name="MyTheme.MainActivity" parent="@style/AppTheme">
        <item name="android:windowBackground">@drawable/app_splash_bg</item>
    </style>

</resources>

AndroidManifest.xml:

...
<activity
    android:name=".MainActivity"
    android:theme="@style/MyTheme.MainActivity"
    ...
    <!-- Set the MainActivity theme, can be skipped if you only have -->
    <!-- one activity and did not setup anything other than your base theme -->

MainActivity.java:

@Override
protected void onResume() {
    super.onResume();
    MainActivity.this.getWindow().setBackgroundDrawableResource(R.drawable.app_bg);
}

Basically, you setup a theme with the splash background for your starting activity. You then set the background to your normal one during onResume of said starting activity.

Also, you can just comment out the windowBackground declaration in the starting activity theme to see the original (I assume) "ugly white" screen on launch.

like image 195
Miao Liu Avatar answered Nov 12 '22 00:11

Miao Liu