Splash screen

A splash screen is the first screen that you see when opening your app. It also signifies that something is loading in the background and you have to wait for a few seconds before landing on the actual screen of the application. 

With the introduction of Android 12, Google team has standardized the way the splash screen is displayed. It shows the app icon, branding icon(optional), and a transition to the app itself. There are few properties that can be customized:

  • Background color :
    <item name="android:windowSplashScreenBackground">#ffffff</item>
  • Animation duration (max 1000ms) :
    <item name="android:windowSplashScreenAnimationDuration">1000</item>
  • Branding icon(optional): 
    <item name="android:windowSplashScreenBrandingImage">@drawable/branding_logo</item>
  • Icon background color :
    <item name="android:windowSplashScreenIconBackgroundColor">#000000</item>
  • Animated icon :
    <item name="android:windowSplashScreenAnimatedIcon">@drawable/animated_icon</item>

When you try to set these properties into your default themes.xml, you will get an error because the new splash screen is available only for Android 12 or higher. Sure, you can set these properties in themes.xml in values-v31 folder, but what with backward compatibility then? 

Google team to the rescue 

Google introduces the Core Splash Screen library, which handles properly displaying splash screen for us. Also, if you have previously implemented splash screen, they recommend to migrate to the new approach, because as we can read in the documentation using old approaches may result in unexpected behavior on Android 12:

If your existing splash screen is implemented using a custom theme that overrides android:windowBackground, the system replaces your custom splash screen with a default Android system splash screen on Android 12 and higher (which may not be your app’s intended experience).

If your existing splash screen is implemented using a dedicated Activity, launching your app on devices running Android 12 or higher results in duplicate splash screens: the new system splash screen displays, followed by your existing splash screen activity.

Migrate your splash screen implementation

  1. First you have to add dependency in build.gradle:
    implementation


    'androidx.core:core-splashscreen:1.0.0-alpha02'
     
  2. Then in default themes.xml, you have to create theme for splash screen (parent theme must be Theme.SplashScreen from the google library):

    <style name="Theme.AppSplash" parent="Theme.SplashScreen">
            <item name="windowSplashScreenBackground">@color/white</item>
            <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
                 <item name="postSplashScreenTheme">@style/Theme.AppTheme</item>
    </style>
     
  3. In parameter postSplashScreenTheme you have to point to the theme that should be applied after splash screen, in my case it’s;

    Theme.AppTheme
     
  4. Set splash theme as a theme of your application or splash screen activity in AndroidManifest.xml:

    android:theme="@style/Theme.AppSplash"
     
  5. Use method installSplashScreen() from the google library before setContentView method in onCreate in your activity: 

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            installSplashScreen()
            setContentView(R.layout.activity_main)
        }
    }
     
  6. Well done! 
     

Android Splash Screen

Share

facebook linkedin
End