How to implement Android Splash Screen



Splash screen are used in large applications to notify the user that the program is in the process of loading. It provide a feedback of underway process. A progress bar indicates the loading progress , Splash screens used to enhance the look and feel of an application. It may have animation, graphics and sound.

Android splash screen are normally used to show some kind of progress to user before app loads completely. Some application uses splash screen just for show case of their app, company logo for a couple of second. The purpose of android splash screen is depends on the application, by this we can show the special features, or load some basic data for the application.

In this tutorial I am going to explain how to implement android splash screen using timer.

1. Create a new project by File => Android Application Project and fill other data.
2. For splash screen we can create a new activity with name SplashScreen.java.
3. Now just open the AndroidMenifest.xml and set the splash screen activity as launcher activity.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tricksofit.splashscreen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.tricksofit.splashscreen.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.tricksofit.splashscreen.MainActivity"
            android:label="@string/app_name" >
        </activity>

    </application>

</manifest>

4. Now create a layout splash.xml. This layout contain a logo.

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:src="@drawable/tricksofit" />

</LinearLayout>

5. Now add the following code in you SplashScreen.java file. In this I have used a handler to wait for 5 seconds and after the timer is out our main activity will launch.

package com.tricksofit.splashscreen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class SplashScreen extends Activity {

	private static final int SPLASHTIME = 5000;
	private static final int STOPSPLASH = 0;
	private Handler mSplashHandler;

	// Handler start
	{
		mSplashHandler = new Handler()
		{
			@Override
			public void handleMessage(Message msg)
			{
				super.handleMessage(msg);
				switch (msg.what)
				{
					case STOPSPLASH:
						Intent intent = new Intent(getApplicationContext(), MainActivity.class);
						startActivity(intent);
						finish();
				}
			}
		};
	}// Handler end

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

		// to start the handler
                mSplashHandler.sendEmptyMessageDelayed(STOPSPLASH, SPLASHTIME);
	}

}

DOWNLOAD CODE

Now run the application, you will see the splash screen for 5 second and then mail activity will launch.

splash screen