Thursday, May 12, 2016

Android Splash Screen using Android Studio

Most of the Android apps uses splash screen before launching their application activity. You can see that with most popular apps such as Skype, Facebook, Dropbox etc.

While I was developing an Android APP POC for one of my customer, the requirement was to create a splash screen with their logo which stays on the screen for 3 seconds. It can be done very easily with the below steps.

First we have to create a new project in the Android studio :
1. Click on File -> New Project
2. Mention the application Name And Minimum SDK, Click on Next
3. Select Blank Activity and Click Next Button
4. Click on Finish

This create the android application with blank activity. Now we will add the Splash screen at the start this application.
First Create a layout to display the Splash screen. We call it as splash.xml.


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


We have to copy the splash_image.jpg image file to drawable folder. This is the image which is going to be shown in the splash screen.

Now create a activity which sets this splash content view. This activity we call it as SplashScreen.java. Put the below code to this class

SplashScreen.java   
package marutsoft.vehicledata;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.widget.ImageView;
 
/**
 * Created by Karjol G T on 11-05-2016.
 */
public class SplashScreen extends Activity {
 
    protected int _splashTime = 3000;
     private Thread splashThread;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Display display = getWindowManager().getDefaultDisplay();
        setContentView(R.layout.splash);
        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent intent = 
                    new Intent(SplashScreen.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        };
        timerThread.start();
    }
 
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
}
  
We have mentioned the sleep for 3000 ms that’s 3 seconds. Here Once the splash screen is done it invokes MainActivity.class.

Now we have to include the splash activity to Manifest and we have to make it as a launcher activity to start the application with this activity. You need to add the below code to the AndroidManifest .xml file. 

<activity
            android:name=".SplashScreen"
            android:label="@string/app_name" >
            <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>    

Now Run this Android application to check the Splash screen running at the beginning of the application.

That’s it. Happy Coding..


No comments: