aherman
aherman

Reputation: 960

Android 12 Splash Screen API customization

Since Android released the new Splash Screen API with Android 12, a lot of apps had issues with duplicate splash screens, lack of customization, etc.

Right now, it is possible to set the background color and icon in the middle of it, but is it possible to customize it a bit more? Since right now we are limited to use single-colored background and non-resizable logo icon which doesn't look quite good.

What I'm trying to achieve is a custom splash screen, with an image drawable as background (or layer-list with 2 items - one background image and one centered logo), as it could be used before Android 12.

Did someone succeed to achieve this type of behavior?

There is a workaround to set windowIsTranslucent attribute to true and show only the second splash (the right one), but it introduces bad UX since it seems like the app is not responding for a few seconds.

Upvotes: 26

Views: 10011

Answers (2)

yozhik
yozhik

Reputation: 5084

Short answer is No, but here in my answer you can find more info:

Android: How to set a drawable as a windowSplashScreenBackground parameter in the new SplashScreen API?

Upvotes: 1

Amirhossein
Amirhossein

Reputation: 323

I did something like this. First remove default drawable

 <style name="Theme.App.Starting" parent="Theme.SplashScreen">
      <item name="windowSplashScreenBackground">@color/...</item>
      <item name="windowSplashScreenAnimatedIcon">@android:color/transparent</item>
      <item name="postSplashScreenTheme">@style/AppTheme</item>
</style>

Then inflate your custom splash view

class MainActivity : AppCompatActivity(), SplashScreen.OnExitAnimationListener {

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          val installSplashScreen = installSplashScreen()
          installSplashScreen.setOnExitAnimationListener(this)
          setContentView(R.layout.activity_main)
      }


      override fun onSplashScreenExit(splashScreenViewProvider: SplashScreenViewProvider) {
           val view = splashScreenViewProvider.view
           if (view is ViewGroup) {
               val binding = ActivityMainSplashBinding.inflate(layoutInflater,view, true)
               // Do what you want with your inflated view
               animate(view) {
                   // Remove splash
                   splashScreenViewProvider.remove()
               }
           }
       }



     private fun animate(view: View, doOnFinish: () -> Unit) {
         view.animate()
        .withEndAction(doOnFinish)
        .start()
     }
}

At the end remember to call splashScreenViewProvider.remove() to remove splash.

Upvotes: 0

Related Questions