How to create a Splash Activity in Android

This Android tutorial demonstrates how to build an Android Application that displays a Splash Screen. In this example the Splash Activity displays at the start of the application and remains there for around three seconds then it automatically goes off the screen and a new Activity or main application starts.

The following example also shows how to use "Message" and "Handler" classes to handle Splash Activity.

As a first step create the Android Project. Select File>New>Android Project in the Eclipse menu.

Now create a class for splash screen that extends the Activity class.

public class SplashActivity extends Activity 

Create another class for main activity that will display after the splash activity go off the screen.  

public class MainActivity extends Activity {}

Create xml layout files.

Create activity_splash.xml in the res/layout folder. This is used as resource layout file for SplashActivity.

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

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:textSize ="18sp"
    android:text="Splash Activity"
    />
</LinearLayout>

Create another resource layout file “activity_main.xml” for MainActivity class.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize ="18sp" android:text="Main Activity" /> </LinearLayout>
	

Open AndroidManifest.xml file and define the tag and attributes for both activities.

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

Open SplashActivity.java file. Create an inner class SplashHandler that extends Handler. Then define a method handleMessage() in this class.

private class SplashHandler extends Handler {

    	public void handleMessage(Message msg)	  { }
}

In the onCreate() method of SplashActivity class create an object of type SplashHandler.

SplashHandler mHandler = new SplashHandler();

Next create a Message object.

Message msg = new Message();

Assign a unique code to this message. Later, this code will be used to identify the message in Handler class.

msg.what = 0;

Send the message with a delay by calling sendMessageDelayed(). It will be processed after a delay of 3 seconds (3000 = 3 seconds).

mHandler.sendMessageDelayed(msg, 3000);

Following code shows the implementation of handleMessage() method of Handler class. The method begins by calling the super. In the next step there is a switch statement to handle the messages. Upon receiving the message of interest it creates a new Intent to start the MainActivity. Call to finish() also finishes the current activity(in this case SplashActivity).

    // Handler class implementation to handle the message
    private class SplashHandler extends Handler {
    	
    	//This method is used to handle received messages
    	public void handleMessage(Message msg)
    	  {
    		// switch to identify the message by its code
    		switch (msg.what)
    	    {
    	    default:
    	    case 0:
    	      super.handleMessage(msg);
    	      
    	      //Create an intent to start the new activity.
    	      // Our intention is to start MainActivity
    	      Intent intent = new Intent();
    	      intent.setClass(SplashActivity.this,MainActivity.class);
    	      startActivity(intent);
    	      // finish the current activity
    	      SplashActivity.this.finish();
    	    }
    	  }
    }    

Final code for SplashActivity.java

public class SplashActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        
        //Create an object of type SplashHandler
        SplashHandler mHandler = new SplashHandler();
        // set the layout for this activity
        setContentView(R.layout.activity_splash);
        // Create a Message object
        Message msg = new Message();
        //Assign a unique code to the message.
        //Later, this code will be used to identify the message in Handler class.
        msg.what = 0;
        // Send the message with a delay of 3 seconds(3000 = 3 sec).
        mHandler.sendMessageDelayed(msg, 3000);
    }     
    
    // Handler class implementation to handle the message
    private class SplashHandler extends Handler {
        
        //This method is used to handle received messages
        public void handleMessage(Message msg)
          {
            // switch to identify the message by its code
            switch (msg.what)
            {
            default:
            case 0:
              super.handleMessage(msg);
              
              //Create an intent to start the new activity.
              // Our intention is to start MainActivity
              Intent intent = new Intent();
              intent.setClass(SplashActivity.this,MainActivity.class);
              startActivity(intent);
              // finish the current activity
              SplashActivity.this.finish();
            }
          }
    }    
   
}

Final code for MainActivity.java

public class MainActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


 

 

Comments

WORKS!!!! Nice example. I just spent hours playing around with others that crashed. Great example m8. By far the best Android Splash Screen Example on google!!!

Great answer and worked very well. Thanks a lot for the post!!!

Saved me some time, taught me some stuff.

Very clear and easy to follow.

I have spent a few days trying and testing several methods of "splash" screens. This one worked right from the start. Thanks.

Good points all aurnod. Truly appreciated.

Norlmaly I'm against killing but this article slaughtered my ignorance.

There's ntohing like the relief of finding what you're looking for.

If time is money you've made me a walteiher woman.

I thank you hmubly for sharing your wisdom JJWY

very helpful. Thank you.

Very good one. need more tutorials.. Thanks

flawless working. thanks a lot! :)

It worked smoothly thanks that has solved my problem. I wish to increase the splash stays on the screen

Surripsignly well-written and informative for a free online article.

Always the best content from these prodigious wtriers.

If time is money you've made me a waelthier woman.

Good and simple example . Keep it up

brilliant, spent a long time looking for stuff this is by far the easiest and most efficient i have come across thank you!

Thanks a lot! Working flawlessly...

To the point! Thanks..

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.