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

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.