Many Applications, mostly games, on the market show splash screens. With this screen they prompt a logo for the application and/or the author.
I will show you a short way to implement a splash screen which will occur on every startup, will stay for a number of seconds you can define, will close on touching the screen and will not reappear on pressing the back button.

I created an empty project named SplashScreen with the activity SplashScreen. This activity will display the splash screen, so we have to create a new activity which will be the first real view you want to display. In my case this activity is named MyApp.

To see, that we see different screens, we copy the content of our layout file main.xml and paste it into a new layout file called splash.xml. Of course we have to change the @string/hello reference to a now one.
To make it short, here the string.xml, the main.xml and the splash.xml

string.xml

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">SplashScreen</string>
    <string name="main_screen">MainScreen</string>
    <string name="splash_screen">SplashScreen</string>
</resources>

main.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/main_screen"
    />
</LinearLayout>

splash.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/splash_screen"
    />
</LinearLayout>

The layouts aren’t beauty but thats not the intention of this how to.

The activity MyApp is really nothing more than a new created activity.

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.droidnova.android;
 
import android.app.Activity;
import android.os.Bundle;
 
public class MyApp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

But now lets go to the more interesting part: the SplashScreen activity itself.

We will work with a thread to realize the function, that the screen will disappear after some seconds. For that we need two class variables. One for the number of time the screen has to appear and the flag, if the thread should run or not.

1
2
protected boolean _active = true;
protected int _splashTime = 5000; // time to display the splash screen in ms

In the onCreate() method, we will create a Thread object and override the run() method dynamically.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
 
    // thread for displaying the SplashScreen
    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }
            } catch(InterruptedException e) {
                // do nothing
            } finally {
                finish();
                startActivity(new Intent("com.droidnova.android.splashscreen.MyApp"));
                stop();
            }
        }
    };
    splashTread.start();
}

As you see from line 13 – 18, we run in a while loop if our flag is true and if our waited variable is less the number we defined in our variable _splashTime.
Finally, we finish our activity, which prevents the restart with the back button, start a new activity which will start our MyApp activity and the last step should be to stop the thread.

At the end we override the function onTouchEvent() and set the _active flag to false to force the thread to stop the while loop and start the MyApp activity immediately.

1
2
3
4
5
6
7
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        _active = false;
    }
    return true;
}

Thats it!

Full Eclipse project: SplashScreen.zip

  • Share/Bookmark