Implement Default Splash Screen Prior to Android 12
Tags: android, beginners, kotlin, jetpackcompose
Published: 2023-02-17 23:00:00

**Step-by-step Guide to Implement Default Splash Screen in Android 12 (API level 31) for Older Android Version**

If your app doesn't implement any splash screen, it is implemented by default in Android 12 (API level 31). In other words, there is a splash screen for Android 12 or later but not for Android 11 and older Android version.

![Implement Default Splash Screen Prior to Android 12 01.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1665107059721/Pkv_R0qWn.gif)

To stay consistent with the older Android version to have a similar default splash screen, you can follow this step-by-step guide to implement the splash screen.

## 1\. Add Splash Screen Library

Add `androidx.core:core-splashscreen` library in **build.gradle**.

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

## 2\. Add Splash Screen Style

Add this **splash.xml** in your **res/values** folder. It can be any name you wish. You can also use the existing **themes.xml** if you have it.

```xml
<resources>
    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">#ff616161</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_android_kotlin_weekly</item>
        <item name="postSplashScreenTheme">@android:style/Theme.Material.Light.NoActionBar</item>
    </style>
</resources>
```

* Set the theme style parent to `Theme.SplashScreen`
    
* Set `windowSplashScreenBackground` to your app primary color
    
* Set `windowSplashScreenAnimatedIcon` to your app icon asset
    
* Set `postSplashScreenTheme` to your original theme style for your app
    

> Please note that I hard code the color instead of using `@color/...` because I have gotten rid of this **colors.xml** completely in this [clean compose app template](https://vtsen.hashnode.dev/clean-empty-jetpack-compose-app-template).

In fact, I think we shouldn't need to have this `splash.xml` at all for Jetpack Compose app.

## 3\. Update Application Theme Style

In **AndroidManifest.xml**, update `application android:theme`

```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:theme="@android:style/Theme.Material.Light.NoActionBar">
    </application>
</manifest>
```

with `@style/Theme.App.Starting` that you created in **splash.xml**

```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:theme="@style/Theme.App.Starting">
    </application>
</manifest>
```

## 4\. Call installSplashScreen() in Activity

In your activity, call the `installSplashScreen()`, before you call the `Acitivty.onCreate()` base class.

To decide how long the splash screen stay, you can call `SplashScreen.setKeepOnScreenCondition()` which allows you to implement `KeepOnScreenCondition` interface using [SAM conversion](https://vtsen.hashnode.dev/implement-kotlin-interfaces-with-sam-conversions).

This is an example of what I did for my [rss feed reader app](https://vtsen.hashnode.dev/simple-rss-feed-reader-jetpack-compose)

```kotlin
    override fun onCreate(savedInstanceState: Bundle?) {

        setupSplashScreen()

        super.onCreate(savedInstanceState)
        /*...*/
    }

    private fun setupSplashScreen() {
        var keepSplashScreenOn = true
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.uiState.collect { uiState ->
                    keepSplashScreenOn =
                        uiState is ArticlesUiState.Loading ||
                        uiState is ArticlesUiState.Invalid
                }
            }
        }

        installSplashScreen().setKeepOnScreenCondition {
            keepSplashScreenOn
        }
    }
```

## Summary

Your app now should have the splash screen in all Android versions. This is a default splash screen implementation which is pretty simple. For more details, you can refer to this [official guide](https://developer.android.com/develop/ui/views/launch/splash-screen/migrate).

> If the splash screen doesn't work, you may want to make sure your app is completely killed because the splash screen is NOT shown during [hot start](https://developer.android.com/topic/performance/vitals/launch-time#hot). This seems to happen in API 33 where pressing the back button doesn't kill the app. It is still in the background.

## Reference

Code Changes Example: [Diff](https://github.com/vinchamp77/AndroidNews/commit/94b006b79ce9fc2f7dc2bf892f71bcd53a7dbd44)(Android News) and [Diff](https://github.com/vinchamp77/Demo_CleanEmptyCompose/commit/65aa3504a0a2d8db0ad06c24c6bfcf1aed90ad1a)(Template App)

---

*Originally published at [https://vtsen.hashnode.dev](https://vtsen.hashnode.dev/implement-default-splash-screen-prior-to-android-12)*.