Sign in
Log inSign up
Splash Screen Migration To Android 12

Splash Screen Migration To Android 12

Migrate existing splash screen to Android 12

Junaid Ahmad's photo
Junaid Ahmad
·Sep 4, 2021·

2 min read

Android 12 Splash.png Android 12 SDK beta version is now available which includes some behavior changes for those apps which are targeting Android 12 SDK. It means that if you are application has compileSdkVersion 31 or above it will start affecting due to these changes. Check here about more Android 12 behavior changes.

When we start our application targeting Android 12, the system always applies this default splash screen to the apps. By default, it will pick the app icon and the background color from the application theme for the splash screen.

The apps with custom splash activity will show duplicated splash screens if running on Android 12 devices.

So let's see how we can migrate our existing Splash Screen to the new Android 12 Splash API.

1. Change SDK Version

Open module build. gradle file and change the following fields.

android {
   compileSdkVersion 31
   buildToolsVersion "30.0.3"

   defaultConfig {
       targetSdkVersion 31
   }
   ...
}
dependencies {
   ...
   implementation 'androidx.core:core-splashscreen:1.0.0-alpha01'
}

Please note that you will need to have a version 4.2 or above of Android Studio for these changes.

2. Create Theme

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
   <item name="windowSplashScreenBackground">@color/...</item>
   <item name="windowSplashScreenAnimatedIcon">@drawable/...</item>
   <item name="windowSplashScreenAnimationDuration">200</item>
   <item name="postSplashScreenTheme">@style/Theme.App</item>
</style>
  • windowSplashScreenBackground This attribute is used for the splash background colour.
  • windowSplashScreenAnimatedIcon This is used for setting animated or just drawable.

  • windowSplashScreenAnimationDuration This is required if you set the animated icon as drawable.

  • postSplashScreenTheme Takes the theme of the Activity that directly follows your splash screen.

3. Change Theme in Manifest file

<application
 ...
 android:theme="@style/Theme.App.Starting">
 <activity 
  android:name=".ui.MainActivity" android:exported="true">
      <intent-filter>
        ...
      </intent-filter>
 </activity>
 ...
</application>

In Android 12 it is mandatory to mention the exported value true/false for activity in Manifest. Since we are using the Intent filter in our activity therefore we set it to true.

I suggest you read more about the show the rationale for data access here

4. installSplashScreen and Exit Animation

class MainActivity: Activity() {
   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       // Handle the splash screen transition.
       val splashScreen = installSplashScreen()

       setContentView(R.layout.main_activity)
       setSplashExitAnimation(splashScreen)
}

private fun setSplashExitAnimation(splashScreen: SplashScreen) {
    splashScreen.setOnExitAnimationListener { splashScreenView ->
        configureObjectAnimator(splashScreenView) { slideUpAnimation ->
            with(slideUpAnimation) {
                interpolator = AnticipateInterpolator()
                duration = 500L
                doOnEnd {
                    splashScreenView.remove()
                }
                start()
            }
        }
    }
}

private fun configureObjectAnimator(
    splashScreenView: SplashScreenViewProvider,
    onComplete: (ObjectAnimator) -> Unit
) {
    val objectAnimator = ObjectAnimator.ofFloat(
        splashScreenView.view,
        View.TRANSLATION_Y,
        0f,
        -splashScreenView.view.height.toFloat()
    )
    onComplete.invoke(objectAnimator)
}

Here is the Github repo for Android Splash Migration github.com/JunydDEV/android-dayplanner-app

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct