fluttersplash-screendynamic-splash-screen

How do you dynamically handle Splash Screen for Android 12 and above devices and Android 11 and below devices? And without a package if possible


So I have trying to add an Image as a splash screen and I have added certain densities files in mipmap as splash.png.

So I want to handle splash screen implementation for both Android 12 and above and for android 12 below, because in android 12 they introduced new SplashScreen API. So I have to handle for both.

So I tried to handle problem by implementing below like this:

The below is the code of res/value/styles.xml:

    <!-- For devices below Android 12 -->
    <style name="SplashTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">@mipmap/splash</item>
    </style>

    <!-- For devices Android 12 or higher -->
    <style name="SplashTheme_S" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="windowSplashScreenBackground">@mipmap/splash</item>
    </style>

The code for AndroidManifest.xml:

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

MainActivity.kt:

package com.myorg.mynotes

import android.os.Build
import android.os.Bundle
import android.util.Log
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("MainActivity", "SDK_INT: ${Build.VERSION.SDK_INT}")
        // Dynamically set splash screen theme based on Android version
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            Log.d("MainActivity", "Using SplashTheme_S for Android 12 or higher")
            setTheme(R.style.SplashTheme_S)
            Log.d("MainActivity", "After setTheme function")
        } else{
            Log.d("MainActivity", "Using SplashTheme for Android versions below 12")
            setTheme(R.style.SplashTheme)
            Log.d("MainActivity", "After setTheme function")
        }
        super.onCreate(savedInstanceState) 
    }
}

I expect above code to handle splash screen dynamically but it is not doing so. What am I doing wrong?


Solution

  • So what I did is I added my image files (splash.png) to the mipmap folders(mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi). Then I modified the code in values/styles.xml and values-night/styles.xml.

    Code changes in values/styles.xml:

    <!-- Old code -->
    <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
    
    <!-- New code -->
    <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">@mipmap/splash</item>
    </style>
    

    Code changes in values-night/styles.xml:

    <!-- Old code -->
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
    
    <!-- New code -->
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@mipmap/splash</item>
    </style>
    

    I also created new folders inside android\app\src\main\res named values-v31 and values-night-v31 and added styles.xml file in both folders.

    Code for values-v31/styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="LaunchTheme" parent="Theme.SplashScreen">
            <item name="windowSplashScreenBackground">@mipmap/splash</item>
        </style>
    
        <style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
            <item name="android:windowBackground">?android:colorBackground</item>
        </style>
    </resources>
    

    Code for values-night-v31/styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="LaunchTheme" parent="Theme.SplashScreen">
            <item name="windowSplashScreenBackground">@mipmap/splash</item>
        </style>
    
        <style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
            <item name="android:windowBackground">?android:colorBackground</item>
        </style>
    </resources>
    

    This solved my problem of adding an image as a splash screen in devices of android 12 and above and in devices of android 11 and below.