Android Splash Screen Example

0

Your application might need to do some kind of configuration tasks in the background as you launch it and this may take a few seconds.

Instead of just leaving it as it is, you may want to show a splash screen.

Android splash screen can be used to show user some kind of progress before the app loads completely. You may want to show your app related background image or a company logo with some progress bar in the foreground.

In android, we don’t have any inbuilt mechanism to show splash screen. In this example, I will show you how to implement splash screen and show it as you load your android application.

An example of splash screen would be a shopping application which may do some background tasks such as checking whether network connection is available, making sure user is registered, if already registered, building up a notification list else taking the use to the login page etc.

Custom Progress Bar

Let’s spice up our example by building our own custom progress bar. We will build a custom progress bar, in the shape of ring, with gradient of colors that will make it appear as if rotating.

Gradient based ring progress bar

Gradient based ring progress bar

splash_screen_loader.xml:

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50.0%"
    android:pivotY="50.0%" >
    <shape
        android:innerRadiusRatio="2.5"
        android:shape="ring"
        android:thicknessRatio="10.0"
        android:useLevel="false" >
        <gradient
            android:centerColor="#2E9AFE"
            android:centerY="0.5"
            android:endColor="#A9D0F5"                        
            android:startColor="#0000FF"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</animated-rotate>

Here is our splash screen layout.
We will specify the above drawable in attribute android:indeterminateDrawable of the <ProgressBar> element.

splash_screen.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/splash_root_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:baselineAligned="false"
    android:background="@drawable/splashscreen">

    <LinearLayout
        android:id="@id/progressbar_loading_layout"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_marginBottom="100.0dip"
        android:layout_weight="0.53"
        android:gravity="bottom"
        android:orientation="vertical">

        <ProgressBar
            android:id="@id/splash_screen_progressBar"
            android:layout_width="32.0dip"
            android:layout_height="32.0dip"
            android:layout_gravity="center_horizontal"
            android:indeterminateDrawable="@drawable/splash_screen_loader" />
    </LinearLayout>

</LinearLayout>

In order to implement splash screen we are going to create a separate activity for splash.
The splash activity will inflate the splash screen layout. Display the progress bar. Launch the tasks in background using Android’s AsyncTask. Here we just simulated the background job by making the thread sleep for few seconds. Once the background job is over, in onPostExecute(), launch our main activity and close the splash activity using finish().

So let’s get started by creating a new project

SplashActivity:

package com.javarticles.android;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

public class SplashActivity extends Activity {
	private ProgressBar d = null;

	@SuppressLint({ "NewApi" })
	protected void onCreate(Bundle paramBundle) {
		requestWindowFeature(1);
		super.onCreate(paramBundle);
		setContentView(R.layout.splash_screen);
		this.d = ((ProgressBar) findViewById(R.id.splash_screen_progressBar));
		if (this.d != null) {
			this.d.setVisibility(0);
		}
		MainLauncher launcher = new MainLauncher();
		launcher.execute(new Void[0]);
	}

	private static final int SLEEP_TIME = 12;

	private class MainLauncher extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... params) {
			doSomeTasks();
			return true;
		}

		protected final void onPostExecute(Boolean paramBoolean) {
			// Start main activity
			Intent intent = new Intent(SplashActivity.this, MainActivity.class);
			SplashActivity.this.startActivity(intent);
			SplashActivity.this.finish();
		}

		private void doSomeTasks() {
			try {
				Thread.sleep(SLEEP_TIME * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void onDestroy() {
		super.onDestroy();
		View localView = findViewById(R.id.splash_root_view);
		if (localView != null) {
			localView.setBackgroundDrawable(null);
		}
	}
}

Here is our main activity.

MainActivity:

package com.javarticles.android;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
	}
}

In android manifest, we will declare both splash and main activities.

androidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javarticles.android"
    android:versionCode="1"
    android:versionName="1.0">
	<uses-sdk
		android:minSdkVersion="14"
		android:maxSdkVersion="22"
		android:targetSdkVersion="22"/>
	<application
        android:icon="@drawable/ic_launcher_sandbox"
        android:label="@string/app_name"
        android:allowBackup="true">
        <activity android:name="com.javarticles.android.SplashActivity"
            android:windowSoftInputMode="adjustResize|stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.javarticles.android.MainActivity"/>
    </application>
</manifest>

Run the splash screen example

Splash Screen With Ring ProgressBar

Splash Screen With Ring ProgressBar

after_splash_main_screen

Download the source code

This was an example about Android Splash Screen.

You can download the source code here: splashScreen.zip
Share.

Comments are closed.