The right way to implement a Splash Screen in Android

SHISHIR
2 min readFeb 17, 2019

--

Splash Screen one of the exciting feature in android, is referred to a welcome screen or user’s first experience of an application. A professionally designed Splash Screen has a possibility of making your Application look more professional.

Unfortunately in android we don’t have any inbuilt mechanism to show splash screen compared to iOS. But there are multiple ways to implement Splash Screen for an Android App. In this article, we will walk through 2 common methods of implementing splash screens and will find the right way:

  1. Using Timers (the bad)
  2. Using a Launcher Theme (The Right way)

Method 1: Using a timers

This is the easiest method where we create a splash activity and create a thread in onCreate() for shows up for 2/3 seconds, then go to our desired activity. Here see the implementation of this easy method:

Method 2: Using a Launcher Theme

Theme for Splash Screen: Do you know when an activity is called Android OS first see in the manifest is there any theme for that activity and load the theme from manifest.??? So we will set a custom theme in Manifest for our splash activity. To create the theme for splash screen follow the below process.

  • Create background for splash screen splash_screen_background.xml in res/drawable directory.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="@android:color/holo_purple" /><item>
<bitmap
android:gravity="center"
android:src="@drawable/app_logo" />
</item>
</layer-list>
  • Create Style for Splash Screen in res/values/style.xml.
<style name="SplashScreenTheme", parent = "Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/launcher_screen_with_logo</item>
</style>
  • Set the style as theme for SplashScreenActivity in AndroidManifest.xml
<activity android:name=".SplashScreenActivity"
android:theme="@style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>.Time for Splash Screen:

The time for Splash Screen:

We have already a theme to show as splash screen. Now need the time for showing it. Okay In method 1, if you notice carefully you will find a white blank screen before opening your splash screen. Do you know why it happens ? This happens because of ‘Cold Start’. ‘Cold Start’ is the time taken from the moment the code is initialized till the UI is responsive to the user. It happens in cases such as your app is being launched for the first time since the device booted, or since the system killed the app.

So, In this method we will use this cold start time to show our splash screen. Here we have no exact time for splash screen rather it will take the cold start time. And when onCreate() will be called, we will redirect to MainActivity() and finish the SplashSreenActivity.

*As we are loading the splash screen them from manifest. So there is no need to setContentView() with any xml layout.

public class SplashScreenActivity extends AppCompatActivity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
finish();
}
}

Happy Coding:)

--

--

SHISHIR
SHISHIR

Written by SHISHIR

{ 'designation' : 'Lead Software Engineer' , 'hobby' : [ 'Music', 'Photography', 'Travelling' ] ,’email’: ‘shishirthedev@gmail.com’ }

Responses (8)