Splash Screen for Android Application - Example

Adding a splash screen for your application is necessary if your application takes more time to load. It is similar to the PC games we play where you would see the brand of the gaming company before the actual game commences.

Here in this example we will create an activity to display your splash screen with progress bar for two seconds . After two seconds the user will be directed to the main page.

So lets start:

1. Create a new project File -> Android Project. While creating a new project name the as SplashScreenActivity(SplashScreenActivity.java).Press Ctrl+Shift+O for missing imports after typing the code

SplashScreenActivity.java


public class SplashScreenActivity extends Activity {
 private long ms = 0;
 private long splashDuration = 2000;
 private boolean splashActive = true;
 private boolean paused = false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.splashscreen);
  Thread mythread = new Thread() {
   public void run() {
    try {
     while (splashActive && ms < splashDuration) {
      if (!paused)
       ms = ms + 100;
      sleep(100);
     }
    } catch (Exception e) {
    } finally {
     Intent intent = new Intent(SplashScreenActivity.this,
       MainActivity.class);
     startActivity(intent);
    }
   }
  };
  mythread.start();
 }
}

2. Now you need to create user interface for the SplashScreenActivity.java
3. Create a new xml file in layout folder as splashscreen.xml

splashscreen.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:src="@drawable/androidbite" />

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/image"
        android:layout_gravity="center_horizontal" >
    </ProgressBar>

</LinearLayout>

4. Now that the splash screen is ready lets create the main activity(MainActivity.java) and layout for the main activity (main.xml)

MainActivity.java

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }
}
main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="181dp"
        android:text="Welcome To Main Screen" />

</RelativeLayout>
5.Here come the most important part, include all your activity in manifest file. In addition to that put android:noHistory=”true” attribute in the SplashScreenActivity's tag so that the activity will not appear in the activity stack, meaning if the user presses back from the main activity its should not direct the user to splash screen.


AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbite.splashscreen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".SplashScreenActivity"
            android:label="@string/title_activity_splashscreen"
            android:noHistory="true" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_splashscreen" />
    </application>

</manifest>


Output:

The output of this example would be as follows