Working with Splash Screen API in Android: Part 1

Dharmesh Basapati
4 min readMar 22, 2022

--

The latest and recommended way for consistent app startup experience

Why

This is the question we ask Google or Android team every time whenever they introduce something which we as a developer thinks — Do we really need this new API?

We are really enjoying our old school way of setting up our custom activities which will act as a splash screen or in simple terms launcher screen for our apps by placing some handler or delay functions to hold that screen for some time and then launch then main app screen. But Google has some other ideas or we can say some better, more futuristic idea in their camps for simple yet crucial splash screen feature for our apps.

Introduction

They introduced the new Splash Screen API for more consistent app startup experience across apps for your users. Starting from Android 12, our apps will show the app icons on initial app startup without us doing anything in that app. But yes, we can customize this whole experience by provide our custom values, drawables, and, colors to this new splash screen thing.

Initially we can update the compileSdkVersion to 31 (if not updated already) in our app level build.gradle file and then launch our app in any Android 12 device to see the magic of new Splash Screen thing happening in our app.

But now if you try to run that same app in below Android 12 devices, then that same magic will not happen as expected because Google has different ways to help you out from different situations like they have introduced this new Splash Screen API or library for backward compatibility which you can implement to raise the bar of your app startup experience.

Let’s check out the steps to implement this new API in our apps:

Step 1: Add this library in your app level build.gradle file and hit “Sync now” -

implementation 'androidx.core:core-splashscreen:1.0.0-beta01'

Step 2: Add this code block to your theme.xml file -

<style name="Theme.AppSplashScreen" parent="Theme.SplashScreen">

// Set the splash screen background.
<item name="windowSplashScreenBackground">@color/white</item>

// Set any animated or static app icon
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_svg_app_icon</item>

// Set the theme of the Activity that directly follows your splash screen.
<item name="postSplashScreenTheme">@style/Theme.MainAppTheme</item>

</style>

Details about properties used in the above code:

  1. windowSplashScreenBackground = The background color of your splash screen. Use it if you want a different and unique background color other than default white or black color according to light or dark modes.
  2. windowSplashScreenAnimatedIcon = This is that static or animated vector drawable which will act as your main app icon. In case, you don’t use this property, your device will take the icon used in the mipmap folders of your app to display as a app icon in new splash screen, but somehow this thing is working fine in Android 12 devices but not proper for below Android 12 devices as they some default android icon instead of your app icon placed in mipmap folders.
  3. postSplashScreenTheme = This property asks for your main app’s theme name which you want to use it after your splash screen thing finishes its job. Please provide value to this property otherwise your app’s overall theming will look scary post splash screen thing happens.
  4. There are other properties also which you can add in here such as windowSplashScreenAnimationDuration, windowSplashScreenIconBackgroundColor, windowSplashScreenBrandingImage.

Note: Please keep your app icon ready in vector drawable format with recommended dimensions mentioned in the official documentation and then add here in the values of this property — windowSplashScreenAnimatedIcon

Step 3: Now update the theme name in the AndroidManifest.xml file with the newly created splash screen theme name -

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppSplashScreen">//Here

</application>

Step 4: Now add this line in your launcher activity’s onCreate() method before setContentView() method -

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen() //This line
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
}

Final Step: Run your app now and if feasible try to run and test your app on Android 12 as well as below Android 12 devices or emulators.

Conclusion

There are many things which I have not covered in this article because my sole reason for putting out this article is to simplify the process of using this API with basic steps if followed properly can provide you the desired result and in this case our desired result is to display that new splash screen across all Android versions. There are many minor details we can customize to display the app icon or branding image with proper dimensions and properties, maybe we can cover that in some other article but for now I think I have covered what I thought at the beginning.

Kudos !!!

--

--

Dharmesh Basapati
Dharmesh Basapati

Written by Dharmesh Basapati

Hi, I'm Dharmesh Basapati, an Android Dev with 8+ years of experience. Dad of 2, husband, cricket fan. Love photography, writing, and creating content.

No responses yet