Android Splash Screen Tutorial

Most of the applications require an introductory opening screen. Such introductory screens are called splash screens. The splash screen displays for 2-4 seconds and then the main activity of the application appears. So how we can implement such splash screen to our apps? This tutorial is all about.

The implementation of splash screen is quite simple. For this, simply add a java class that displays the splash screen. The splash screen uses a thread to wait for 2-4 seconds and then it uses an intent to start the next activity.

Here is our step by step tutorial-

1. Create a new project by going to File ⇒ New Android Project and fill required details:
  
    Project Name : SplashScreen
    Package : com.sunil.splash
    Main Activity : Splash.java
    Layout : splash.xml
    Build Target : Android 2.2 
 
2. Open the res/layout/splash.xml file and insert the following code:

<?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="wrap_content"
        android:src="@drawable/background" />

    <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>

3. Now under res folder, create a new folder drawable and drag and drop a background image for splash screen ( Rename image as per splash.xml eg. background.png ).

4. Now under res/layout/ folder, create a new layout file home.xml and add the following code:

<?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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dip"
        android:text="HomeScreen"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

 5. Now open the Splash.java file and add the following code:

package com.sunil.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class Splash extends Activity {
    private long ms=0;
    private long splashTime=2000;
    private boolean splashActive = true;
    private boolean paused=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Hides the titlebar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.splash);

        Thread mythread = new Thread() {
            public void run() {
                try {
                    while (splashActive && ms < splashTime) {
                        if(!paused)
                            ms=ms+100;
                        sleep(100);
                    }
                } catch(Exception e) {}
                finally {
                    Intent intent = new Intent(Splash.this, Home.class);
                    startActivity(intent);
                }
            }
        };
        mythread.start();
    }
}

6. Create a new Activity Home.java under com.sunil.splash package and add the following code:

package com.sunil.splash;

import android.app.Activity;
import android.os.Bundle;

public class Home extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

    }
}

7. Now open AndroidManifest.xml file and define the Home.java Activity inside <application> tag. eg.

<activity
            android:name=".Home"
            android:label="@string/title_activity_home" >
        </activity>

The second thing we need to change in AndroidManifest.xml file is to set - android:noHistory="true" property in the definition of Splash.java Activity. This property helps in finishing the application when we click back button to back to the splash screen. eg.

<activity
            android:name=".Splash"
            android:noHistory="true"
            android:label="@string/title_activity_splash" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


8. Finally run your project. You will see something like this:


Android Splash Screenhome screen


That's it ! You can also download the source code of this tutorial. 

Download Code






3 Comments:

Zeeshan Zakaria said...

Thanks for this tutorial.

I am using JellyBean. It works perfectly, however pressing the 'Back' button goes back to the splash screen. I added a finish() before the new Intent is called, and now it works fine.

Kaushal Fauzdar said...

Great Tutorial ! Works perfectly :)

Snehal Bornare said...

thanks this example is very helpful for me!!!!

Post a Comment

Recent Posts

Site Stats

Our Apps

Get it on Google Play

©2012-2014 Android Aspect - All rights reserved | Powered by Blogger

Creative Commons License