Recently I wrote a blog article on splash screens for Android apps. I didn’t take the time then to build a simple demo app with source code so I’m doing so now.
There is not really much to my Splash Demo app. I took the basic Hello world app and added a splash screen. The splash screen stays visible for about 5 seconds and then switches to the Hello activity. The app looks like the following:
Source Code
Download the source code for the Eclipse project from this page at wglxy.com:
http://www.wglxy.com/android-tutorials/splash-screen-demo-app-for-android
If you find that the app does not build in Eclipse, be sure to do a clean build by using the “Clean” item on the Project menu. The project was built with Android 2.2.
Building the Demo
Here are steps I took to build this demo app.
- I started with the default Android project that displays “hello”.
- I added a LauncherActivity and changed the Android manifest file to start that activity rather than the MainActivity.
- I built an image, 512 x 512, to use as the splash image.
- I added a layout xml file for LauncherActivity that displays the image on a black background.
- I changed the onCreate method of the LauncherActivity so it displays its view for 5 seconds and then starts the MainActivity.
- I added an AsyncTask class as an internal class of LauncherActivity in order to have a background task triggering the transition to the MainActivity.
The last two steps require a bit more explanation.
The onCreate method of LauncherActivity is the place where the transition to the main activity happens. It does two things. It sets up to handle clicks on the image view showing the splash image. That provides a user with a quick way to skip the splash screen. The second thing onCreate does is start a second task. The GoHomeAfterDelay class is a subclass of AsyncTask. It runs in the background, or, in this case, it sleeps in the background. When it awakes, the task finishes and triggers a call to start the main activity. Note also that I put the actual time of delay, 5000 milliseconds, in a values.xml resource file as the integer named splash_screen_time.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); setContentView(R.layout.launcher); // Set up to go to the home screen if a user touches the image. ImageView i1 = (ImageView) findViewById (R.id.splash_image); if (i1 != null) i1.setOnClickListener(this); // Set up to go to the home screen after a short delay. int delay = getResources ().getInteger (R.integer.splash_screen_time); mDelayTask = new GoHomeAfterDelay (this); mDelayTask.execute (delay); }
The GoHomeAfterDelay class is a subclass of AsyncTask. Its definition is at the bottom of the LauncherActivity.java file. GoHomeAfterDelay’s doInBackground method does nothing more than go to sleep for the time specified. Part of the protocol for any AsyncTask involves executing its onPostExecute method when the task completes. So that’s the spot where we end the launcher activity and start the main activity.
/**
* Perform the task, which is to wait N milliseconds.
* Return 1 if the wait completed or 0 if the task was cancelled.
*/
@Override protected Integer doInBackground(Integer... delayValues)
{
int delay = delayValues [0];
try {
Thread.sleep (delay);
} catch (InterruptedException ex) {} if (isCancelled ()) return 0;
else return 1;
}
/**
* Report that the task has finished.
* This method runs in the UI thread.
*/
@Override protected void onPostExecute (Integer moveCount)
{
if (! mCancelled) {
if (mActivity != null) mActivity.goHome (true);
}
mActivity.mDelayTask = null;
}
If you are unfamiliar with AsyncTask and the reason for it, I refer you to two earlier blog notes: AsyncTask note , Long Computations note.
public void goHome (boolean doFinish)
{
final Intent intent = new Intent(this, MainActivity.class);
intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity (intent);
if (doFinish) finish ();
}
Acknowledgements
As I said earlier, there are several good articles about splash screens already out there. My favorite is the one on “Android Splash Screens Done Right“. Thanks go to Mr. Clifton for sharing this very useful information.



Thanks a lot for this. I will be using it on the next version of our android guitar tuner app as the sounds are loading up.
Great Stuff!
whats the first thing I need to do too build a splash screen for my zte.
jphillipk@hotmail.com
I like to think about what first impression you want to leave with the user and then try to come up with several candidate images for a splash screen.