Google’s recommended way to implement Splash-Screen in your Android App

Abhishek Gururani
3 min readSep 30, 2022

--

Thumbnail

Let’s quickly understand why do we need a splash-screen in our apps, startup of any android application can be divided into three types,

source : developer.android.com

During the cold and warm start, the activity is yet to be created, and for the time being the screen of the application is “white”, in order to cover-up for that “white” screen a splash-screen is shown.

The most “naive and non-recommended” way to show a splash-screen is by first creating a dummy splash-screen MainActivity and then delaying it for some time using Handler(), before you move on to your real MainActivity.

But this way isn’t cool, as it doesn’t solve the problem of “white” screen during the cold/warm start. Infact it may lead to cold start, because the app will now take some extra time to load the layout of this dummy splash-screen MainActivity.

Instead of this we’ll use the latest Android Splash-Screen API.

step 1 : include Splash-Screen compat library in your app's build.gradle

Android Splash-Screen API

step 2 : add a new style inside of values -> themes -> themes.xml

create a new style

there are three attributes here, windowSplashScreenBackground(for setting the bkg), windowSplashScreenAnimatedIcon(for splash-screen icon), postSplashScreenTheme(to set the theme to be shown post splash-screen theme).

Here, my background is : white , icon is pre-stored : mipmap logo and my post splash-screen theme : Theme.YourMainActivityName

step 3 : go to your app's AndroidManifest.xml and add this "Theme.App.Starting" to your application element and activity sub-element,

adding theme

step 4 : simply call the function, installSplashScreen(), and voila!!

inside MainActivity calling installSplashScreen() function before onCreate(…)

This now shows splash-screen(THE RIGHT WAY) before the MainActivity, but only for a fraction of milli-second. If you want splash-screen to be shown for longer period(for any custom time), here are the steps,

installSplashScreen() returns the splash-screen object, which you can optionally use to customize animation or keep the splash-screen on screen for a longer duration.

~ developer.android.com

step 5 : create a ViewModel,

A ViewModel is just gonna do some dummy work in a coroutine and attach the splash-screen object to it using setKeepOnScreenCondtion. And till the ViewModel does the dummy work in background, your splash-screen will be up, and MainActivity's onCreate method will be delayed.

add the required ViewModel dependency,

dependency required to use ViewModel class

before super.oncreate(...),

attaching splashScreenObject to viewModel,
MainViewModel Class, inherits viewMode() class

setKeepOnScreenCondition, will keep the splash-screen on the screen till the condition is true, once it becomes false(here, after the delay of 800 millisecond) splash-screen will be removed. In order to delay for 800 milli seconds, I'm using coroutines.

Here is a quick video of an application that implements splash-screen using the above mentioned way,

teaser of an application I’m working on, yaay!!

I hope you had a wonderful time, and you’ve learned how to implement splash-screen using the google recommended splash-screen API.

Viva-La-Vida.

--

--

Abhishek Gururani

I write articles on Android Development, and problem solving.