Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 510
Siraj Sumra Avatar asked Mar 03 '23 05:03

Siraj Sumra


1 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.

like image 102
Achref ArShavin Avatar answered Mar 05 '23 18:03

Achref ArShavin