Monday, 9 September 2013

Android Splash Screen Example with Animation Tutorial

Android Splash Android Splash
  1. When user click on application icon application is take some time or couple of seconds to load.
  2. During this time user know your application is loading so that time splash screen is important thing in our application
  3. Many application start with splash screen we know that very well because in Game app and other application have a splash screen.
  4. The purpose of splash screen is to user know that application is loading. Does not give more delay time to load splash screen because user does not want more time to wait for application load.
  5. For Creating splash screen you need one image of launch screen we have taken 480x800 here for our example you can see Android Developer Site for Different sizes of splash for all device resolution.
  6. Follow below step by step process of creating Splash Screen for Android Application
  1. Create new Android Project call Android Splash Screen and put your launch image at res/drawable folder.
  2. Also create three xml file one for splash screen give name splashscreen.xml in res/layout folder and create one new folder in res folder and give name anim and create two xml file called fade_in.xml and fade_out.xml for animation.
  3. Modify Animations xml file as per below code.

    fade_in.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="4000"/> //Milliseconds  
     </set>  
       
    

    fade_out.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="4000"/> //Miliseconds  
     </set>  
       
    
  4. Now modify your main.xml and splashscreen.xml file as per below code

    main.xml
     <?xml version="1.0" encoding="utf-8"?>  
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent"  
       android:layout_height="fill_parent"  
       android:background="#FFFFFF"  
       android:gravity="center"  
       >  
       
       <TextView  
         android:id="@+id/tv1"  
         android:gravity="center"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_marginTop="70dp"  
         android:textSize="20sp"  
         android:text="@string/main_screen"  
         android:textColor="#000000" />  
       
     </RelativeLayout>  
    

    splashscreen.xml
     <?xml version="1.0" encoding="utf-8"?>  
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent" android:background="@drawable/splash_screen"  
       android:layout_height="fill_parent"  
       android:orientation="vertical" >  
       
     </RelativeLayout>  
    
  5. Now Create one SplashScreen.java file in src folder and modify as below code

    SplashScreen.java
     package com.idroid.splashscreen;  
       
     import android.app.Activity;  
     import android.content.Intent;  
     import android.os.Bundle;  
     import android.os.Handler;  
       
     public class SplashScreen extends Activity {  
       
          private static final int TIME = 4 * 1000;// 4 seconds  
       
          @Override  
          protected void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.splashscreen);  
       
               new Handler().postDelayed(new Runnable() {  
       
                    @Override  
                    public void run() {  
       
                         Intent intent = new Intent(SplashScreen.this,  
                                   MainActivity.class);  
                         startActivity(intent);  
       
                         SplashScreen.this.finish();  
       
                         overridePendingTransition(R.anim.fade_in, R.anim.fade_out);  
       
                    }  
               }, TIME);  
                 
               new Handler().postDelayed(new Runnable() {  
                     @Override  
                     public void run() {  
                         }   
                      }, TIME);  
       
          }  
       
            
          @Override  
          public void onBackPressed() {  
               this.finish();  
               super.onBackPressed();  
          }  
     }  
       
    
  6. Now build and run your project on Simulator you can see the splash screen for 4 seconds.


Download Source Code :Android_SplashScreen_Demo.zip