How to create android splash screen

In this tutorial I’m going to show you how to easily create an android splash screen in your application.

The whole idea of this sample implementation is to start and display an activity for a certain amount of time and then start new activity. Our activity code is listed below.

SplashActivity.java

package com.itcuties.tutorial.android;

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

public class SplashActivity extends Activity {

   private static String TAG = SplashActivity.class.getName();
   private static long SLEEP_TIME = 5;    // Sleep for some time

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      this.requestWindowFeature(Window.FEATURE_NO_TITLE);    // Removes title bar
      this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);    // Removes notification bar

      setContentView(R.layout.splash);

      // Start timer and launch main activity
      IntentLauncher launcher = new IntentLauncher();
      launcher.start();
   }

   private class IntentLauncher extends Thread {
      @Override
      /**
       * Sleep for some time and than start new activity.
       */
      public void run() {
         try {
            // Sleeping
            Thread.sleep(SLEEP_TIME*1000);
         } catch (Exception e) {
            Log.e(TAG, e.getMessage());
         }

         // Start main activity
         Intent intent = new Intent(SplashActivity.this, MainActivity.class);
         SplashActivity.this.startActivity(intent);
         SplashActivity.this.finish();
      }
   }
}

As you see the implementation is really simple. We have a private class which is responsible for waiting some amount of time and launching a main activity in a new thread. We construct and start this thread at the end of onCreate method when a view has been set.

Remember that SplashActivity is the starting activity of our application so don’t forget to set android.intent.category.LAUNCHER category in Your AndroidManifest.xml file. Our sample manifest file code looks like this.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.itcuties.tutorial.android"
         android:versionCode="1"
         android:versionName="1.0" >

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

   <application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name=".SplashActivity">
         <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/title_activity_main" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
         </intent-filter>
      </activity>
   </application>

</manifest>

Download Eclipse project containing this samples here.

13 Responses to "How to create android splash screen"

  1. neema says:

    thanks, works great ,good tutorial.

    Reply
  2. suma says:

    thanx its working.
    how to use webservices from other server

    Reply
  3. Prashant says:

    hi,
    I have tried xml parsing code from your tutorial but its showing som error in ——————->>>

    public void onItemClick(AdapterView parent, View view, int pos, long id) {

    Intent i = new Intent(Intent.ACTION_VIEW);

    i.setData(Uri.parse(listItems.get(pos).getLink())); // error as method getLink() is undefined for the type Object

    activity.startActivity(i);
    }

    please tell me the mistake ..

    Reply
  4. Arvind says:

    Another easy way using Timer class

    public class SplashActivity extends Activity{
    	Context context;
    	Timer splashTimer;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.splash_screen);
    		context= this;
    		splashTimer =  new Timer();
    		splashTimer.schedule(new TimerTask() {
    
    			@Override
    			public void run() {
    				updateValue();
    			}
    		}, 1000, 15000);// 1 second for initial start value and 15 seconds to display splash screen
    		
    	}
    	public void updateValue()
    	{
    		
    		Intent intent =  new Intent(context, .class);
    		startActivity(intent);
    		splashTimer.cancel();
    		finish();
    	}
    
    }
    
    Reply
  5. Dave says:

    I’m having a small issue with your code – more than likely on my end.
    The splash screen shows and goes on to my main screen as per the tutorial
    but the “Loading…” text does not show on the splash screen.
    I’ve tried using a background image as well as a button on the splash screen but none appear only
    a white blank screen. No errors appear in the code in compiling or running.
    The background that im trying to use on the splash screen appears perfectly on the main screen

    Any help would be much appreciated

    Reply
  6. Willie says:

    Can we make the splash welcome screen only to show up when the customer do a FRESH install? Instead of showing up every time when the app is being updated?

    Reply
  7. wasim says:

    nice tutorial but why new class require we can create new thread and do same thing in it ..

    Reply
  8. Kroshandowski says:

    Hello!How paste .gif image for animation?Just static picture or text not that what I need.
    Thanks in advance.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Java by Example App is available at Google Play Store NOW