Android Splashscreen


Howto implement a splashscreen in android

Frozen Egg
Frozen Egg - Market link
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)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"...>
  3.     <application ...>
  4.       <activity android:name=".Splash"
  5.                   ...
  6.                   android:theme="@android:style/Theme.NoDisplay">
  7.             <intent-filter>...</intent-filter>
  8.      </activity>
  9.      <activity android:name=".FrozenBubble"
  10.                   ...
  11.                   android:theme="@android:style/Theme.NoDisplay" />
  12.    </application>
  13. </manifest>

The splash activity (Splash.java)

The classname FrozenBubble.class can be changed into your main activity.

  1. public class Splash extends Activity {
  2.         @Override
  3.         public void onCreate(Bundle icicle) {
  4.                 super.onCreate(icicle);
  5.                 LayoutInflater inflater = getLayoutInflater();
  6.                 View layout = inflater.inflate(R.layout.splashscreen,
  7.                                 (ViewGroup) findViewById(R.id.splash));
  8.                 Toast toast = new Toast(getApplicationContext());
  9.                 toast.setGravity(Gravity.CENTER_VERTICAL, , );
  10.                 toast.setDuration(Toast.LENGTH_LONG);
  11.                 toast.setView(layout);
  12.                 toast.show();
  13.  
  14.                 new Handler().post(new Runnable() {
  15.                         @Override
  16.                         public void run() {
  17.                                 Splash.this.startActivity(new Intent(Splash.this, FrozenBubble.class););
  18.                                 Splash.this.finish();
  19.                         }
  20.                 });
  21.         }
  22. }

The splash layout (splashscreen.xml)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout id="@+id/splash"
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <ImageView id="@+id/splashscreen" android:layout_width="wrap_content"
  8.           android:layout_height="wrap_content" android:src="@drawable/splash"
  9.           android:layout_gravity="center"/>
  10. </FrameLayout>

Trackback URL for this post:

http://www.2030.tk/trackback/250

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...

rac's picture

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?

rac's picture

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!

rac's picture

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).

rac's picture

Interesting approach... have

Interesting approach... have to look into this when I need it next time ;-)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Use <fn>...</fn> to insert automatically numbered footnotes.
  • You can use the <go> tags just like the <a> for nicer urls.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.