What is Splash Screen in Android ?

  • Splash screen is the first graphical notification you receive when you visit any app. It can even appear as an introductory screen of an application.It also signifies that you have to wait for a few seconds before landing on the actual screen of the application.
  • Splash screen usually appears in full screen that covers the entire screen. It is used to make visitors login before getting access to the app/site, as a method of advertising, allowing visitors to choose their native language or lower bandwidth version of the app. Every good app uses splash screen that gives them a unique identity.
  • Splash screen in a nutshell, is the first impression creators.
  • Splash screens are used to display some animations (i.e. application logo) and illustrations while some data for the next screens are fetched.

Purpose of a Splash Screen

The purpose of a Splash Screen is to quickly display a beautiful screen while the application fetches the relevant content if any (from network calls/database).

Typically, the Activity that has the following Intent filter set in the AndroidManifest.xml file is the Splash Activity.

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

There are Two ways to create the initial screen i.e. Splash Screen of the application. Let’s see each of them.

  • Splash Screen Classical Approach

  • Splash Screen Correct Approach

Splash Screen with Classical Approach

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@android:color/black"
    tools:context=".SplashActivity"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/logopng"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:textStyle="bold"
        android:textSize="22sp"
        android:textColor="#fff"
        android:text="Splash Screen Classical Approach"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@id/imageView" />

</androidx.constraintlayout.widget.ConstraintLayout>

SplashActivity.java

package com.codedecode.yashpwr;

import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

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

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // This method will be executed once the timer is over
                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, 5000);
    }
}

Let’s keep the MainActivity.java empty for now.

AndroidManifest.xml for Splash Screen Classic Approach.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codedecode.yashpwr">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"/>
    </application>
</manifest>

The output produced from the above implementation of SplashScreen is given below.

Splash Screen with Classical Approach

Did you see the blank page that came up before the Splash Screen was visible to you?

The above approach isn’t the correct approach. It’ll give rise to cold starts.

With the above approach, there’s an additional overhead that the SplashActivity uses to create its layout.

It’ll give rise to slow starts to the application which is bad for the user experience (wherein a blank black/white screen appears).

Splash Screen with Correct Approach

The cold start appears since the application takes time to load the layout file of the Splash Activity. So instead of creating the layout, we’ll use the power of the application theme to create our initial layout.

Application theme is instantiated before the layout is created. We’ll set a drawable inside the android:windowBackground attribute that’ll comprise of the Activity’s background and an icon using layer-list as shown below.

splash_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/black" />
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/logopng" />
    </item>
</layer-list>

We’ll set the following style as the theme of the activity.

styles.xml

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_background</item>
</style>

SplashActivity.java

package com.codedecode.yashpwr;

import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_splash); //i commented this line because we don't need activity_splash.xml anymore

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // This method will be executed once the timer is over
                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, 5000);
    }
}

AndroidManifest.xml for Splash Screen Correct Approach. add android:theme:@style/SplashTheme to AndroidManifest.xml at SplashActivity.

<activity android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Final AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codedecode.yashpwr">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"/>
    </application>
</manifest>

Splash Screen with Correct Approach

Using the theme and removing the layout from the SplashActivity is the correct way to create a splash screen.

This brings an end to android splash screen tutorial. You can download the final Android Splash Screen Project from the link below.

Download Code

Happy Coding 🙂