A Comprehensive Guide To Using The New SplashScreen API

yogesh
BYJU’S Exam Prep Engineering
4 min readAug 25, 2022

Hi, there! In this article we will learn how to build Android splash screen using SplashScreen API introduced in Android 12. We will be comparing the old way of adding a splash screen with the new way.

What is Splash Screen ?

Splash screen is the initial screen you’ll see when you open your app. It is also called as launch screen or start up screen. If the app while launching shows blank screen for a moment, it probably doesn’t have a splash screen.

Why is Splash Screen needed in apps ?

A good splash screen is a differentiator between a basic app and a professional one. Besides that, other reasons being:

  • Android apps don’t launch very fast, especially for the first time. There is a delay where splash screen can really help you catch the attention of the user, thus it can be used for promoting the app and display your logo.
  • It can also be used to fetch data before users enters your home screen to give him a smoother experience.

Why is there a need to use a new API ?

Up until now creating a splash screen was a tedious and required quite a bit of boilerplate code to be written like creating a new SplashScreenActivity, adding timers, handling data downloading, etc. It also slowed down the app. But now the new Splash Screen API allows one to avoid all that and helps create splash screens much more easily and efficiently.

How to add the splash screen API to your project?

  1. Change compileSdk to 31 in the module level build.gradle file and add the SplashScreen API as a dependency to your project, using the compat library that wraps the API and provides back support for older versions of Android.
android {
compileSdk 31
...
}
dependencies {
...
implementation 'androidx.core:core-splashscreen:1.0.0'
}

2. Create a new Theme (e.g Theme.App.Starting) and set its parent to Theme.SplashScreen or Theme.SplashScreen.IconBackground

  • Point 1 represents the icon of the screen. (windowSplashScreenAnimatedIcon)
  • Point 2 represents the background colour of the splash screen icon. (windowSplashScreenIconBackgroundColor)
  • Point 3 represents the background colour of the splash screen .(windowSplashScreenBackground)
  • Point 4 represents the space for branding logo if needed.(windowSplashScreenBrandingImage)
<style name="LauncherTheme" parent="Theme.SplashScreen">

<item name="windowSplashScreenBackground">@color/splashColor</item>

<item name="windowSplashScreenAnimatedIcon">@drawable/splash_icon</item>
<item name="windowSplashScreenAnimationDuration">300</item><item name="windowSplashScreenBrandingImage">@drawable/brandingIcon</item> <item name="postSplashScreenTheme">@style/LaucherTheme</item></style>
  • windowSplashScreenBackground: Background color of the splash screen. Defaults to the theme’s ?attr/colorBackground. It specifies solid colour for window background.
  • windowSplashScreenAnimatedIcon: Replaces the icon in the center of the screen and doesn’t need to be animated. Default icon is app’s launcher icon.
  • windowSplashScreenAnimationDuration: splash screen icon can be both static and animated. thus duration is required to specifiy how long the animation should be shown on the screen. The recommended duration for app icon animation is that it shouldn’t exceed 1000 milliseconds.
  • windowSplashScreenBrandingImage: it can be used to set an image to be shown at the bottom of the splash screen. The design guidelines recommend against using a branding image
  • postSplashScreenTheme: It specifies the theme of the activity that follows the splash screen immediately. It’ll be the theme of your home screen.

3. Open AndroidManifest file and add a theme to the activity.

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

4. To use the splash screen features, call installSplashScreen() method before super.onCreate() within the activity which starts immediately after the splash screen. In our case, it is HomeActivity.

class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

5. build and run 🎉

Performance Improvement

Startup Time can be measured by using Time to initial display metric from logcat. Logcat includes an output line containing a value called Displayed. This value represents the amount of time elapsed between launching the process and finishing drawing the corresponding activity on the screen

old avg startup time: 1m 106 ms

new avg startup time: 625ms

So using the new splash screen api we got ~2x faster startup time in android 12 devices.

Some Useful links

  • Specs for splash screen icons : link
  • Migrate from old splash screen : link
  • Measure the startup time to imporve performance: link

Things I disliked about the api.

  • windowSplashScreenBackground supports only single colour and there is no support to add drawable yet.
  • There is no fixed guidelines for the image which makes it more of a hit and error game. You can only try adding padding to the image until it fits.
  • There is no splash screen icon displayed when using deeplinks or notifications

--

--