Android SplashScreen

Helder Pinhal
Helder Pinhal
Jul 23 2021
Posted in Engineering & Technology

Android 12 & the new Core SplashScreen API

Android SplashScreen

Amongst the many new features in Android 12 is the new Splashscreen API, which allows us to improve the user experience of our apps. Historically, we would implement some form of splash screen, but now we have an official Google solution to this design principle.

In the latest version of Android, this splash screen behaviour is added to all apps without any action, and we can customise a few visual properties. However, to bring this functionality to apps running Android 6+, we can use the first alpha version of the Core Splashscreen API introduced by Google on June 30th as part of the AndroidX libraries.

But before we get into the implementation, what exactly is a splash screen?

What is a splash screen?

A splash screen is a short-duration visual effect that can be displayed upon an app’s launch from the home screen when an app loads, instead of displaying a blank screen. Displaying a splash screen can decrease the sense of a long load time, and has the potential to enhance the user experience. The official feature showcase has a great illustration to demonstrate this:

Splash screen example
Splash screen example from Android Developers

The system shows the splash screen using the themes and the animations, if any, that you've defined. Once the app is ready, the splash screen is dismissed and the app is displayed.

When the user launches the app, this screen is shown under two circumstances:

  1. During a cold start — when the app process is not running.
  2. During a warm start — when the app process is running, but the system has destroyed your activity, and it needs to be recreated.

When the app is launched but the system hasn't destroyed your activity, a.k.a. hot start, the splash screen will not be shown.

Implementation

To use the Core Splashscreen API, your app needs to be compiled with Android 12 (API 31). At the time of writing, it's still in beta which means no API level number has been assigned to it, yet.

Update your build.gradle to compile against Android 12 and add the dependency on core-splashscreen:

android {
    compileSdkVersion 'android-S'
    // compileSdkVersion 31
}

dependencies {
    implementation 'androidx.core:core-splashscreen:1.0.0-alpha01'
}

First and foremost, we need a new theme to customise our splash screen. Here's how you can do it, and update the properties to match your requirements:

<!-- NOTE: you should already have a main theme. -->
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
      <!-- Your theme attributes goes here -->
</style>

<style name="Theme.MyApp.SplashScreen" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/blue</item>
    <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
    <item name="postSplashScreenTheme">@style/Theme.MyApp</item>
</style>

Now that we have a theme for the splash screen, we need to assign it to the Activity that will handle it. Assuming this is our MainActivity, adjust your Manifest.xml like shown below:

<application>

    <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.MyApp.SplashScreen" />

</application>

Lastly, there's a very important step we need to perform before this is working. We need to install the splash screen in our MainActivity before setting the content view during onCreate(). This will take care of replacing the theme back to what we've specified in the postSplashScreenTheme attribute in the splash screen theme.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Install the splash screen BEFORE setContentView
        installSplashScreen()

        setContentView(R.layout.activity_main)
    }
}

And that's it. We now have an API 23 backwards compatible splash screen. 🎉

Conclusion

A splash screen is important to bring some life to that boring empty screen when an app is launched. Not only that but apps feel snappier thus improving the user experience.

Historically there were various approaches to add a splash screen to an app. It's great that we now have a Google backed solution, unifying how this functionality should be implemented. Additionally, Google didn't just make the feature available in the latest Android version. Instead, they gave us backwards compatibility until Android 6.0.

If you would like to know more about this subject, you can take a look at the official feature introduction and API documentation in Android Developers.

As always, we hope you liked this article and if you have anything to add, we are available via our Support Channel.

Keep up-to-date with the latest news