How to place the Splash screen for Dark and Light mode in Android?

Viewed 3025

In the Kotlin based Android app, I have a Splash screen which I have developed with the Style attributes as below code:

Drawable File(This one is for Light, Same is for Dark with different gradient color codes):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:angle="270"
                android:endColor="#43dc91"
                android:startColor="#29abe2" />
        </shape>
    </item>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/My_Image" />
    </item>
</layer-list>

Style also has 2 style.xml files - one for dark and one for light:

 <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_back</item>
    </style>

And it is called from the Manifest:

<activity
            android:name="com.app.ui.splash.SplashActivity"
            android:theme="@style/SplashTheme">

Now the issue is that when my app switches to the dark mode from light mode, user will close the app and then open the app again, for 2 seconds, it still shows the light mode splash and after 2 seconds, the dark mode splash will load.

This could happen because when the app is loaded in the splash activity -> Oncreate, I am verifying that if the app has a preference of dark theme then load the dark mode and then it switch my app to dark mode as below:

private fun setAppTheme() {
        when {
            userHolder.theme != null -> {
                if (userHolder.theme == string_(R.string.text_dark_mode))
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                else AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            }
            else -> {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
            }
        }
    }

What to do in such a case? How to load dark mode splash in the first instance itself while loading it from style and drawable?

3 Answers

I faced the same issue as you and finally, I realized that at the moment there is no way to do that for Android versions not containing the dark mode feature. So for older versions, you will see the light splash screen even if you enable your dark mode theme in your app with

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

You can test this behavior too in many other applications like WhatsApp and Messenger in Android 9 and below, it will show you the light splash screen even if you turned on the dark mode inside the app. May it helps someone for saving his time.

There is actually a simple solution to this. Make your dark mode splash screen your default. I personally believe that the seeming convention of white/brightly colored splash screens is terrible.

I hate it when I open an app in a dark room and I'm blinded by for a couple of seconds as my eyes adjust. It should be industry standard not to have brightly colored splash screens. Use soft/darker hues for your default background.

I realize this is more of an opinion but it solves your problem on older devices by not having to choose as your default is already your darker splash screen.

Related