Android Splash Screen Example

In this post, we will see how to create Android splash screen.
Android splash screen is nothing but screen that appears when some background task (such as fetching data from database,loading images) is going on. These kind of task generally tasks long and at that time, we can show splash screen. Splash screen can be app icon, company logo , punch line. Once background task completes, another activity will get called.
For example:
When Xender app loads, you will see screen as below.
Xender App

Source code:

Lets create Android splash screen example:

Step 1 :Creating Project

Create an android application project named "SplashScreenExampleApp".

Step 2: Put any image in drawable folder with name "splash_logo"

Step 3 : Creating splash screen layout file

  • Go to res -> layout
  • Right click on layout
  • Click on New -> File. 
  • Create a file named "splash_screen.xml" and paste below code in splash_screen.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/splash_logo"
        android:layout_gravity="center"/>
</LinearLayout>

Step 4: Create SplashActivity

package com.java2blog.splashscreenexampleapp;

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

import com.java2blog.splashscreenexampleapp.R;

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        //creating new thread just for demonstration of background tasks
        Thread t=new Thread() {
            public void run() {

                try {
                    //sleep thread for 10 seconds
                    sleep(10000);

                    //Call Main activity
                    Intent i=new Intent(SplashActivity.this, com.java2blog.splashscreenexampleapp.MainActivity.class);
                    startActivity(i);

                    //destroying Splash activity
                    finish();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        //start thread
        t.start();
    }
}
Create SplashActivity as above. We are creating a new thread and calling sleep for 10 secs on it. In real time, this will be replaced by actually task that will be performed such as fetching data from database or loading images.

Step 5 : Creating Layout

Change res ->layout -> activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.java2blog.splashscreenexampleapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World from Java2blog.com" />
</RelativeLayout>

Step 6 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:
package com.java2blog.splashscreenexampleapp;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.java2blog.splashscreenexampleapp.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
It is default MainActivity provided by HelloWorld app.

Step 7 : Changing AndroidManifest.xml

We need to put launcher activity as SplashActivity, so we need to do following changes in app -> src -> main -> AndroidManifest.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2blog.splashscreenexampleapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.java2blog.splashscreenexampleapp.SplashActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <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>

Step 7 : Running the app 

When you run the app, you will get below screen:

Android Splash Screen tutorial
After 10 secs, you will get below screen:


Android Splash Screen Example

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012