Android Splash Screen Tutorial

Many Android Applications start with Splash Screen.Such as Game applications have app logo or company logo in splashscreen.Splash Screen is a view that appears for sometimes (3 or 4 seconds).After dislaying splash screen that never comes second time in application until we restart Application.

The purpose of a splash screen is to give the user an indication that your app is loading–not to create an extra delay before the user can load your app. People don’t like waiting, especially in a mobile environment.

Using Thread,AsyncTask,Handler you can wait sometimes to load your app.The most desirable way to display splash screen is handler.you can do it easily.no need to create thread or asynctask in your activity.The hadler have postDelayed(Runnable,long)  method.using this method you can wait some times to load app.

Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

new Handler().postDelayed(new Runnable() {
  @Override
  public void run() {
         } 
    }, SPLASH_TIME);

Now we have Runnable interface as thread and also have parameter SPLASH_TIME.when you enter long value in splash_time its milliseconds.when handler is called its wait for time first that executes the run method.so we have to put code for next screen in run method.

Create SplashScreen Activity in your project as a first Activity. and put your logo in splash.xml file as background of main layout.then add content view setContentView(R.layout.splash).Now put hadler code in oncreate method.Then put intent in run method.

Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);

Now when start activity we need to finish SplashScreen Activity.Because when you came back no need to see splash screen.Using finish() method you can remove activity for activity stack. or put nohistory in activity in manifest file.

SplashScreen.this.finish();
or
<activity android:name=".SplashScreen" android:noHistory="true"  />

now Main part of splash screen is animation between two activities.Using overridePendingTransition(start animation,end animation).so create folder anim inside res folder of your project and put animation xml file.

overridePendingTransition(R.anim.fadein,R.anim.fadeout);

res->anim->fadein.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:fillAfter="true">
     <alpha android:fromAlpha="0.0"
            android:toAlpha="1.0"
            android:duration="5000"/> //Time in milliseconds
</set>

res->anim->fadeout.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fillAfter="true">
     <alpha android:fromAlpha="1.0" 
            android:toAlpha="0.0"
            android:duration="5000"/> //Time in milliseconds
</set>

so total code for animation in SplashScreen is  and put it in onCreate  method and dont forgot to finish activity in onBackpressed method::

new Handler().postDelayed(new Runnable() {
public void run() {
Intent mainIntent = new Intent(SplashScreen.this,MainActivity.class);
                        startActivity(mainIntent);
               overridePendingTransition(R.anim.mainfadein,R.anim.splashfadeout);
}
}, SPLASH_TIME);

DownLoad Full Source Code From Here
        DownLoad Source Code

4 comments:

  1. good article , i like it. good work
    Adnan Fakhar
    velocitykhan@hotmail.com

    ReplyDelete
  2. Thanks for this splash screen tutorial! brilliant. I compiled a list of some top resources around this topic- I included your post. Hope other developers find this useful too. Check it out, feel free to share. http://www.verious.com/board/Giancarlo-Leonio/building-an-android-splash-screen/

    ReplyDelete

Android Testing App