Android Splashscreen
Howto implement a splashscreen in android


While working on the easter theme for Frozen Bubble (Frozen Egg => market://search?q=pname:ch.racic.frozenegg) I searched for a way to show a splashscreen while the app was loading.
The first implementation of a splash screen was to start a small activity that shows a splash image for a certain time period and the starts the main activity but this still shows the black screen while loading the main activity.
I remembered that toast messages can be customized and can contain images, thats my second implementation that I want to show to you.
Feel free to comment if you have better ways or ideas how to implement a splashscreen better.
Code listings after the break...
In the AndroidManifest.xml I just declared the Splash.java class as main activity, the real main activity as a second activity and declared android:theme="@android:style/Theme.NoDisplay" for both activities.
AndroidManifest.xml (just interesting parts)
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"...>
-
<application ...>
-
<activity android:name=".Splash"
-
...
-
android:theme="@android:style/Theme.NoDisplay">
-
<intent-filter>...</intent-filter>
-
</activity>
-
<activity android:name=".FrozenBubble"
-
...
-
android:theme="@android:style/Theme.NoDisplay" />
-
</application>
-
</manifest>
The splash activity (Splash.java)
The classname FrozenBubble.class can be changed into your main activity.
-
public class Splash extends Activity {
-
@Override
-
public void onCreate(Bundle icicle) {
-
super.onCreate(icicle);
-
LayoutInflater inflater = getLayoutInflater();
-
(ViewGroup) findViewById(R.id.splash));
-
Toast toast = new Toast(getApplicationContext());
-
toast.setGravity(Gravity.CENTER_VERTICAL, , );
-
toast.setDuration(Toast.LENGTH_LONG);
-
toast.setView(layout);
-
toast.show();
-
-
@Override
-
public void run() {
-
Splash.this.startActivity(new Intent(Splash.this, FrozenBubble.class););
-
Splash.this.finish();
-
}
-
});
-
}
-
}
The splash layout (splashscreen.xml)
-
<?xml version="1.0" encoding="utf-8"?>
-
<FrameLayout id="@+id/splash"
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<ImageView id="@+id/splashscreen" android:layout_width="wrap_content"
-
android:layout_height="wrap_content" android:src="@drawable/splash"
-
android:layout_gravity="center"/>
-
</FrameLayout>
Trackback URL for this post:
- Download PDF
- Printer-friendly version
- 10213 reads

Line 7
Also line 7 is confusing. How can you do a findViewById for a view that has not been inflated yet? In other words, in line 6 you are inflating R.layout.splashscreen so the view corresponding to R.id.splash does not exist yet for findViewById to work...
Line 6 and 7 are one line
Line 6 and 7 are one line for java.
View layout = inflater.inflate(XXX, (ViewGroup) findViewById(YYY)); <= this means get me the view element YYY from the Layout file XXX.
R.layout.splashscreen points to the res/layout/splashscreen.xml and the R.id.splash is an id that is contained in this splashscreen.xml.
Please explain new Handler().post(new Runnable ...)
Thanks for this useful post.
I am trying to figure out how exactly the blank screen is avoided. The trick lies in new Handler().post(...) line of course. By reading the documentation for Handler I was not able to figure it out.
The Runnable is posted onto the message queue of the current thread which is executing the Toast. Why does that accomplish what we need?
To avoid the black screen,
To avoid the black screen, another method is using android:theme="@android:style/Theme.NoDisplay" in the Manifest xml for your main activity.
I will mention this in my post but it's not usable if your main activity needs a theme.
I can't actually explain why but this trick worked also for other GUI stuff on Android to avoid troubles, I have to investigate and play around with it some more to explain :-).
If I don't use the handler the screen of the main activity just stays blank without loading the content.
Genius!
We're two weeks into an Android project, it's due for delivery in a few days.
This is one of the items on my to do list.
Much appreciated!
The backside with this toast
The backside with this toast splash is that you don't have timing control after you launched the toast and no interaction with the loading process but it avoids the black only screen if the onload method of the main activity has to load more graphics.
Actually, what you can do is
Actually, what you can do is set up a reference back to the splash screen activity using the Application object, then when the 'real' activity finishes loading it can call back to the Splash activity and cancel the toast. (You will also want to set this reference to null so that the Splash activity can be garbage collected).
Interesting approach... have
Interesting approach... have to look into this when I need it next time ;-)
Post new comment