How to add Splash Screen Android

Author: | Posted in Android, Quick Tips No comments

In this tutorial we will learn how to add a Splash Screen to an Android application.

Splash Screen Android :

Splash Screen can used to show as a loading screen before app launches. we can use Splash Screen to load some data from server or just as a show case screen.
In our previous tutorial we learned How to create First Android Application.Now we will add a simple show case Splash Screen to it.
It will show a splash logo before app starts.

Let’s start and Add a layout in /res/layout/ folder with name splash.xml

and paste this code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/splash_logo" />
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginBottom="10dp" />
</RelativeLayout>

In this layout we are showing a splash logo which could be a company/brand logo using an ImageView in center of Screen and a ProgressBar at bottom of Screen. Put splash_logo image in /res/drawable/ folder.

Now add a Activity class to our Package in /src/ folder with name SplashActivity.java

and paste this code

package com.example.myfirstapp;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class SplashActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread thread = new Thread() {
            @Override
            public void run() {
                synchronized (this) {
                    try {
                        wait(4000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        startActivity(new Intent(getBaseContext(),
                                MainActivity.class));
                        finish();
                    }
                }
            }
        };
        // start thread
        thread.start();
    }
    @Override
    public void onBackPressed() {
    }
}

In this code we have used a Thread to open a new Activity after a time interval.
We have  Override a blank onBackPressed method to prevent user from exiting while Splash Screen is showing.

We need to change in our AndroidManifest.xml file also as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name" >
            <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/app_name" >
        </activity>
    </application>
</manifest>

Now Application start with Splash Screen. Run application and check.