Implementing Core Splashscreen API

Igor Escodro
ProAndroidDev
Published in
4 min readJul 4, 2021

--

Android 12 brought us a new feature to improve the user experience, the official Splash Screen support. Previously, if the app wants this visual feature, it might need to add a custom implementation, leading to several different ways to do it. The latest version of Android adds it automatically for all apps and allows customization using simple parameters, but unfortunately this functionality was restricted only to Android 12.

However, on June 30, 2021 we were introduced to the first alpha of Core Splashscreen API, providing backward compatibility for the new Splash Screen APIs. Using this library it is possible to implement this new feature down to API 23, giving the same look and feel across a wide variety of Android versions.

But before we dive in the implementation, what is exactly a Splash Screen?

What is a Splash Screen?

A Splash Screen is a visual feedback which is shown for the user while the app is initializing. This animation provides a better user experience because the user does not stare at an empty screen, but rather it can see the application icon or a custom animation. An example provided in the official docs can illustrate it better:

Splash screen example from Android Developers

This screen is shown at two conditions:

  • During a cold start — when the app process is not running and the user opens the app
  • During a warm start — when the app process is running but the Activity has not been created (or was previously destroyed)

It’s important to mention that the Splash Screen will not be shown during a hot start. It means that, for example, if the user presses the home button bringing the app for background and reopens it before the system destroys the Activity, the Splash Screen will not be shown.

Implementing the Splash Screen API

The new Core Splashscreen API will only work if the application is compiled in Android 12 (API 31). Once this version is currently in preview, we need to update the compileSdkVersion to this version in the build.gradle file. In the future, when Android 12 stable version is released, the version to be used is API 31 .

android {
compileSdkVersion "android-S" // while Android 12 is in preview
// compileSdkVersion 31 - when the Android 12 is stable
}

Still, in this file we add the dependency for this new library:

implementation "androidx.core:core-splashscreen:1.0.0-alpha01"

After syncing the Gradle Files, we need to update our themes/values.xml (and the also the one in night/ if the app supports dark theme) with the new parameters.

First of all, we create a new theme, in our example it’s called Theme.MySplash. Now we add the following parameters:

  • Style parent needs to be Theme.SplashScreen from the API
  • windowSplashScreenBackground to set the background color
  • windowSplashScreenAnimatedIcon to set the splash icon
  • postSplashScreenTheme to set the theme that will be used once the Splash screen is no longer visible

Now we set the application or main Activity theme to the new Splash Screen one:

After setting up the themes, a very important step is to call the splash screen install before the setContentView in the Activity, or other view operation on the root view (e.g setting flags). It will associate the Activity with the postSplashScreenTheme that we defined earlier replacing the Splash Screen theme with the regular one.

If this step is not followed, the application will fail with:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

And that’s it! Now we have the Android 12 Splash Screen back-ported down to API 23. 🎉

Splash screen API example

Final thoughts

When developing apps several times we need to give user a better experience, especially during the first run. This first impression might define if the user will use your app or not. Adding a Splash Screen for a visual feedback that your app is loading will help give a better impression, rather than only showing an empty screen for a few milliseconds.

I worked in some applications where we needed to implement a Splash Screen and once there was not an official library to do so, we tried a few implementations. During the process we faced some issues like it showing during the hot start or the animation not running smooth in some devices. Having an official solution by Google and the possibility to contribute it sharing thoughts and bugs on IssueTracker will create a powerful solution for everyone involved.

For more information about Splash Screens, please access the official docs. It contains info about the concept, parameters and more customization options.

What’s new in Android 12 — Splash Screens

Thanks a lot for reading my article! ❤️

--

--

Passionate Android developer | Google Developer Expert for Android