Monday 1 September 2014

Splash Screen Tutorial in Android



SPLASH SCREEN TUTORIAL IN ANDROID :


Splash screens are used to show company logo or just for the purpose of give the better look and feel in android applications. It is sometimes also used to do a background task like load data from a website, download a file etc and once that type of tasks are finished user will be forwarded to the next activity through the use of Intents.


In this example I am showing you how to give a timer to the splash screen and than how to switch to another activity with the use of Intent.

Create a new project in Eclipse as File => New => Android Application Project and than fill the required details of the project. I am giving the name of package as com.javalanguageprogramming.splashscreendemo. You can give your own name.

Create an Empty Activity during the creation of project and name it as SplashScreenActivity and name the xml file as activity_splash_screen.

Now copy the image shown below in res => drawable.


Copy the code shown below in activity_splash_screen.xml :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
    >  
   
   <ImageView  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent"  
     android:src="@drawable/splashscreen"  
     android:scaleType="fitXY"  
      />  
   
 </LinearLayout>  

In the above code we are only showing an ImageView whose source is the image you just pasted in the drawable.


Now make another xml file in the same folder and name it as activity_main which is used when the splash screen switches after some time.

The code for the activity_main.xml is shown below :
 <?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" >  
     
   
   <TextView   
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent"  
     android:textSize="26dp"  
     android:layout_gravity="center"  
     android:gravity="center"  
     android:text="Visit : http://javalanguageprogramming.blogspot.com"  
     android:textStyle="bold"  
     />  
 </LinearLayout>  
   


Open SplashScreenActivity.java and copy the code shown below :

 package com.javalanguageprogramming.splashscreendemo;  
   
 import android.app.Activity;  
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.view.Window;  
   
 public class SplashScreenActivity extends Activity {  
   
        
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           requestWindowFeature(Window.FEATURE_NO_TITLE);  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_splash_screen);  
             
             
           new Thread(new Runnable(){  
   
                @Override  
                public void run() {  
                     // TODO Auto-generated method stub  
                     try{  
                          Thread.sleep(4000);  
                     }catch(Exception e){  
                          e.printStackTrace();  
                     }  
                     Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);  
                     SplashScreenActivity.this.finish();  
                     startActivity(intent);  
                }  
                  
           }).start();  
      }  
 }  
   


In this class we are running a thread because we cannot do time consuming task in the main thread and fire an Intent after 4 sec or 4000 msec to MainActivity.class.

The code for MainActivity.java is shown below :

 package com.javalanguageprogramming.splashscreendemo;  
   
 import android.app.Activity;  
 import android.os.Bundle;  
   
 public class MainActivity extends Activity{  
   
        
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           // TODO Auto-generated method stub  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
      }  
        
   
 }  
   


Make sure to add MainActivity in the AndroidManifest.

The code for AndroidManifest is shown below :
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.javalanguageprogramming.splashscreendemo"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="19" />  
   
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.javalanguageprogramming.splashscreendemo.SplashScreenActivity"  
       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="com.javalanguageprogramming.splashscreendemo.MainActivity"  
       android:label="@string/app_name" >  
         
     </activity>  
   </application>  
   
 </manifest>  
   

2 comments :