Android App Development: Creating Splash Screen

In this tutorial, you’ll learn how to create a splash screen which can be used to show your logo and play an intro song.

Firstly create the default blank android project with the default main activity.
For this splash screen we are using a background image (1080×1920) called ‘splash.png’, which is placed in the ‘res/drawable-hdpi’ folder. Also place a song called ‘splashsound.mp3’ in the ‘res/raw’ folder.

Create the ‘splash.xml’ file in the ‘res/layout’ with the following content:

< ?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="@drawable/splash" >  
</linearlayout>

Then add the Splash.java file as below to your package in the src folder.

package com.codinglogbook.example;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;


public class Splash extends Activity {

    MediaPlayer introSong;


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        // background music - simpler to setup
        introSong = MediaPlayer.create(Splash.this, R.raw.splashsound);

        introSong.start();

        // SoundPool used for small clips 1 or 2 seconds
        final Thread timer = new Thread() {

            @Override
            public void run() {
                try {
                    sleep(2000);
                } catch (final InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    final Intent openStartPoint = new Intent(".MAINACTIVITY");
                    startActivity(openStartPoint);
                }
            };
        };
        timer.start();
    }


    @Override
    protected void onPause() {
        super.onPause();
        introSong.stop();
        finish();
    }
}

Finally update the ‘AndroidManifest.xml’ to include the splash activity as the launcher and the main activity as the defalut:

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

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

    <application android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity android:name=".MainActivity"
           android:label="@string/app_name" >
            <intent -filter>
                <action android:name=".MAINACTIVITY"></action>

                <category android:name="android.intent.category.DEFAULT"></category>
            </intent>
        </activity>
        <activity android:name=".Splash"
           android:label="@string/app_name" >
            <intent -filter>
                <action android:name="android.intent.action.MAIN"></action>

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

</manifest>