Open In App

Correct Way to Add a Splash Screen in Android

Improve
Improve
Like Article
Like
Save
Share
Report

Google previously advised against utilizing a Splash Screen for Android Applications. It was pointless. Then, when they released the Material Design Specifications, several of us noticed that the Splash Screen had been renamed Launch Screen. Ideal for displaying your brand. So, what is the distinction between a terrible LaunchScreen and a good LaunchScreen?

So you’re undoubtedly asking, “How difficult can it be to create the ideal splash screen?” And you’re probably correct – we’re talking about a screen that will only be displayed for a few seconds (if that). And a screen that seems to flash a logo at the user before disappearing. So it can’t be that complicated. Nope!

Step by Step Implementation

Step #1: Creating a Background XML

To make a proper Splash Screen, you must first build a background that will display your brand’s logo on the screen. You must create an XML file in the res/drawable folder for this.

XML




<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorAccent" />
    <item>
        <bitmap android:src="@drawable/gfgLogo"
                android:gravity="center" />
    </item>
</layer-list>


Step #2: Creating a dedicated custom theme for your Launch Screen

The following step is to design a unique theme for your Splash Activity. 

GeekTip: The theme should be inherited from Theme.AppCompat.NoActionBar, because we don’t want that status bar visible when we are showing Launch.

XML




<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorNative">@color/colorNative</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
  
    <style name="LaunchTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/background</item>
    </style>
</resources>


Step #3: Finally some code cooking

Now comes the fun part, where we control the launch screen, in the right way! The best part here is that unlike the conventional methods to add a Splash Screen there is no need to specify a content view for the SplashActivity because the SplashTheme will be applied to it. So, all you have to do is start your Android application’s MainActivity and then end the SplashActivity by using the finish() function. Here is the activity:

Java




import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
  
public class LaunchScreen extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, LandingActivity.class);
        startActivity(intent);
        finish();
    }
}


Step #4: Finally, setting up the theme

Now we have cooked everything, the only thing left now is to apply the theme for our activity which we created earlier in Step #2, that is pretty easy, just pop the Android.Manifest file and edit it the following way.

XML




<?xml version="1.0" encoding="utf-8"?>
    package="com.geeksforgeeks.launchcreen">
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
  
        <activity android:name=".LaunchScreen" android:theme="@style/LaunchTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LandingActivity" />
    </application>
</manifest>


And just like that you have the Splash Screen in the right manner, this method is so good that even if your app gets a cold start, even then there is no white theme, because the Android OS fetches the theme from the Manifest first, which makes up the time for the Splash!



Last Updated : 23 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads