Building Splash Screen Natively, Android 12, Kotlin
Mohit SharmaPublished Sep 23, 2021 • Updated May 12, 2022
Rate this code example
In this article, we will explore and learn how to build a splash screen with SplashScreen API, which was introduced in Android 12.
It is the first view that is shown to a user as soon as you tap on the app icon. If you notice a blank white screen (for a short moment) after tapping on your favourite app, it means it doesn't have a splash screen.
Often, the splash screen is seen as a differentiator between normal and professional apps. Some use cases where a splash screen fits perfectly are:
- When we want to download data before users start using the app.
- If we want to promote app branding and display your logo for a longer period of time, or just have a more immersive experience that smoothly takes you from the moment you tap on the icon to whatever the app has to offer.
Until now, creating a splash screen was never straightforward and always required some amount of boilerplate code added to the application, like creating SplashActivity with no view, adding a timer for branding promotion purposes, etc. With SplashScreen API, all of this is set to go.
Even for the new
SplashScreenAPI, we need to create a theme but in the
value-v31folder as a few parameters are supported only in Android 12. Therefore, create a folder named
resfolder and add
And before that, let’s break our splash screen into pieces for simplification.
- Point 1 represents the icon of the screen.
- Point 2 represents the background colour of the splash screen icon.
- Point 3 represents the background colour of the splash screen.
- Point 4 represents the space for branding logo if needed.
Now, let's assign some values to the corresponding keys that describe the different pieces of the splash screen.
In case you want to use an app icon (or don't have a separate icon) as `windowSplashScreenAnimatedIcon`, you ignore this parameter and by default, it will take your app icon.
Tips & Tricks: If your drawable icon is getting cropped on the splash screen, create an app icon from the image and then replace the content of
windowSplashScreenAnimatedIcondrawable with the
windowSplashScreenBrandingImage, I couldn't find any alternative. Do share in the comments if you find one.
Open AndroidManifest file and add a theme to the activity.
In my view, there is no need for a new
activityclass for the splash screen, which traditionally was required. And now we are all set for the new Android 12 splash screen.
Adding animation to the splash screen is also a piece of cake. Just update the icon drawable with
AnimatedVectorDrawabledrawable and custom parameters for the duration of the animation.
Earlier, I mentioned that the new API helps with the initial app data download use case, so let's see that in action.
In the splash screen activity, we can register for
addOnPreDrawListenerlistener which will help to hold off the first draw on the screen, until data is ready.
Tips & Tricks: While developing Splash screen you can return
addOnPreDrawListener, so the next screen is not rendered and you can validate the splash screen easily.
I really like the new
SplashScreenAPI, which is very clean and easy to use, getting rid of SplashScreen activity altogether. There are a few things I disliked, though.
- The splash screen background supports only single colour. We're waiting for support of vector drawable backgrounds.
- There is no design spec available for icon and branding images, which makes for more of a hit and trial game. I still couldn't fix the banding image, in my example.
- Last but not least, SplashScreen UI side feature(
theme.xml) is only supported from Android 12 and above, so we can't get rid of the old code for now.
You can also check out the complete working example from my GitHub repo. Note: Just running code on the device will show you white. To see the example, close the app recent tray and then click on the app icon again.
Hope this was informative and enjoyed reading it.