Showing Advance Splash Screen Android Application

Author: | Posted in Android, Quick Tips No comments

In this tutorial we will learn how to show Advance Splash Screen in an Android application.

Advance Splash Screen Android :

In our previous tutorial we learned How to show a simple basic Splash Screen in Android Application.Now we will perform a background operation while showing Splash Screen in an Android application.
It will show a splash logo before app starts and load background data.

We can use our previous splash layout as layout file or can add a new layout in /res/layout/ folder with name splash.xml

and paste this code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/splash_logo" />
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginBottom="10dp" />
</RelativeLayout>

In this layout we are showing a splash logo which could be a company/brand logo using an ImageView in center of Screen and a ProgressBar at bottom of Screen same as we done in our previous tutorial. Put splash_logo image in /res/drawable/ folder.

Now we need to change in our Activity class in /src/ folder with name SplashActivity.java

add a class LoadBackgroudData in our Activity class or create a separate file. this is an AsyncTask class to perform data load operation in background.

    class LoadBackgroudData extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            // Perform load data method here
            // It may be a network operation(loading data from server)
            // or simply loading data from local database
            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            // after execution go to second activity
            startActivity(new Intent(getBaseContext(), MainActivity.class));
            finish();
        }
    }

It would be better if we check for internet connection availability if we are performing any network task. to check for internet connection availability we need to add a method in our activity class.

    public boolean isWorkingInternetPersent() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getBaseContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
        }
        return false;
    }

if we are checking for connection state in our app then we have to add a ACCESS_NETWORK_STATE permission in our application’s Manifest file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

and also add INTERNET permission in Manifest file as

<uses-permission android:name="android.permission.INTERNET"/>

now final output of our activity class

package com.example.myfirstapp;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
public class SplashActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        if (isWorkingInternetPersent()) {
            (new LoadBackgroudData()).execute();
        } else {
            showBasicSplash();
        }
    }
    private void showBasicSplash() {
        Thread thread = new Thread() {
            @Override
            public void run() {
                synchronized (this) {
                    try {
                        wait(4000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        startActivity(new Intent(getBaseContext(),
                                MainActivity.class));
                        finish();
                    }
                }
            }
        };
        // start thread
        thread.start();
    }
    public boolean isWorkingInternetPersent() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getBaseContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
        }
        return false;
    }
    class LoadBackgroudData extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            // Perform load data method here
            // It may be a network operation(loading data from server)
            // or simply loading data from local database
            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            // after execution go to second activity
            startActivity(new Intent(getBaseContext(), MainActivity.class));
            finish();
        }
    }
    @Override
    public void onBackPressed() {
    }
}

in this code we have used a AsyncTask to open a new Activity After a task execution.
we have  Override a blank onBackPressed method to prevent user from exiting while Splash Screen is Showing.

we need to change in our AndroidManifest.xml file also as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SplashActivity"
            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=".MainActivity"
            android:label="@string/app_name" >
        </activity>
    </application>
</manifest>

 

Now Application start with Advanced Splash Screen. Run application and check.