Right Way to create Splash Screen on Android


As we all know splash screen is the user’s first experience of your application. It normally used to display some kind of progress before the application setup completely. As per Google material design spec, Splash Screen follows a pattern known called Launch Screen. You can find the specification here. 

Common Mistake

In most of the application developers use splash screen to showcase brand icon or picture for couple of seconds. This is common practice which most of the developers are following. It is not a good idea to use a splash screen that wastes a user’s time. This should be strictly avoided.

With the common approach you may also lead the problem of blank white page appears during splash launching.


Right Way

The right way of implementing a splash screen is a little different. In the new approach specify your splash screen’s background as the activity’s theme background.

Also the root cause of blank white page problem is that your layout file is visible only after app has been initialized completely.

Do not create a layout file for splash activity. Instead, specify activity’s theme background as splash layout.


Implementation

To implement above approach, first create an XML drawable (splash_background.xml) inside res/drawable folder:

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

    <
item android:drawable="@color/colorPrimary" />

    <
item>
        <
bitmap
           
android:gravity="center"
           
android:src="@mipmap/ic_launcher" />
    </
item>

</
layer-list>

In the next step, set splash_background.xml as your splash activity’s background in the theme. Add a new SplashTheme for your splash activity:

<resources>

    <!-- Base application theme. -->

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/colorPrimary</item>

        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

        <item name="colorAccent">@color/colorAccent</item>

    </style>


    <!-- Splash Screen theme. -->

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">

        <item name="android:windowBackground">@drawable/splash_background</item>

    </style>

</resources>


Configure SplashTheme as your splash activity’s theme in your AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.androidjavapoint.splashscreen">


    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        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=".HomeActivity" />

    </application>

</manifest>

Create an empty activity for Splash without XML layout file. This class will simply redirect to home activity.

package com.androidjavapoint.splashscreen;

import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Start home activity
        startActivity(new Intent(SplashActivity.this, HomeActivity.class));

        // close splash activity
        finish();

    }

}

Notice that we do not have setContentView() for this SplashActivity. View is displaying from the theme and this way it is faster than creating a layout.

If you look at the time splash screen displays is exactly the same with the time taken by app to configure itself because of a cold launch (very first launch). If the app is cached, the splash screen will go away almost immediately.

Hope using this article you will be able make your splash screen work in a right way.


All the code explained is available on GitHub.

To find more interesting topics on Software development follow me at https://medium.com/@ankit.sinhal


You can also find my Android Applications on play store


Comments

Popular posts from this blog

Android Performance: Avoid using ENUM on Android

Android O: Impact On Running Apps And Developer Viewpoint

Smart way to update RecyclerView using DiffUtil