Hi All,
Today I am going to share a tutotial and a sample demo of Android 12
Splash Screen Api. Android 12 Splash screen api introduce in Android 12.
Android 12 adds the SplashScreen API, which enables a new app launch animation
for all apps when running on a device with Android 12 or higher. This includes an
into-app motion at launch, a splash screen showing your app icon, and a transition
to your app itself.
Note: As Splash Screen introduced in Android 12 so it will work on Android
Api level 31 and above.
We can Customize the Splash Screen Theme:
- With the help of below attribute we can customize our Splash activity theme.
1. android:windowSplashScreenBackground : Used for Splash Screen background color.
2. android:windowSplashScreenAnimatedIcon: Used to replace an icon in
the center of the starting window. If the object is animatable and drawable through AnimationDrawable and AnimatedVectorDrawable, you also need to set windowSplashScreenAnimationDuration to play the animation while showing the
starting window.
-
@drawable/splash_screen_animateable_icon
3. android:windowSplashScreenAnimationDuration : Used to indicate the duration of the splash screen icon animation. Max duration is 1000ms.
- 800
4. android:windowSplashScreenIconBackgroundColor : Used to set a background behind the splash screen icon. This is useful if there isn’t enough contrast between the window
background and the icon.
5. android:windowSplashScreenBrandingImage: We can use this to set an image to be shown at the bottom of the splash screen. The design guidelines recommend against using a branding image.
Keep the splash screen on-screen for longer periods:
The splash screen is dismissed as soon as your app draws its first frame. If you need to load a small amount of data such as in-app settings from a local disk asynchronously, you can use ViewTreeObserver.OnPreDrawListener to suspend the app to draw its first frame.
If your starting activity finishes before drawing (for example, by not setting the content view and finishing before onResume), the pre-draw listener is not needed.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
content = findViewById(android.R.id.content)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Log.d("MainActivity", "onCreate: I AM RUNNING ON API 12 or higher")
content.viewTreeObserver.addOnPreDrawListener(object :
ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean =
when {
mainViewModel.mockDataLoading() -> {
content.viewTreeObserver.removeOnPreDrawListener(this)
true
}
else -> false
}
})
}
}
Customizing Splash Screen Animation: In Android 12 we get the Splash screen instance by calling getSplashScreen() from Activity class. So we can further customize
the animation of the splash screen through
Activity.getSplashScreen().
splashScreen.setOnExitAnimationListener { splashScreenView ->
val slideUp = ObjectAnimator.ofFloat(
splashScreenView,
View.TRANSLATION_Y,
0f,
-splashScreenView.height.toFloat()
)
slideUp.interpolator = AnticipateInterpolator()
slideUp.duration = 200L
// Call SplashScreenView.remove at the end of your custom animation.
slideUp.doOnEnd { splashScreenView.remove() }
// Run your animation.
slideUp.start()
}
Code: MainActivity.kt
package com.example.android12splashscreenapi
import android.animation.ObjectAnimator
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewTreeObserver
import android.view.animation.AnticipateInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.core.animation.doOnEnd
import androidx.lifecycle.ViewModelProvider
class MainActivity : AppCompatActivity() {
lateinit var content: View
lateinit var mainViewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
content = findViewById(android.R.id.content)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Log.d("MainActivity", "onCreate: I AM RUNNING ON API 12 or higher")
content.viewTreeObserver.addOnPreDrawListener(object :
ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean =
when {
mainViewModel.mockDataLoading() -> {
content.viewTreeObserver.removeOnPreDrawListener(this)
true
}
else -> false
}
})
// custom exit on splashScreen
splashScreen.setOnExitAnimationListener { splashScreenView ->
val slideUp = ObjectAnimator.ofFloat(
splashScreenView,
View.TRANSLATION_Y,
0f,
-splashScreenView.height.toFloat()
)
slideUp.interpolator = AnticipateInterpolator()
slideUp.duration = 200L
// Call SplashScreenView.remove at the end of your custom animation.
slideUp.doOnEnd { splashScreenView.remove() }
// Run your animation.
slideUp.start()
}
}
}
}
Reference:
Note: For updating/migraating your older app for Android 12 please check below
tutorial.
Download Complete Code
HereHope this will help Someone.
Enjoy Coding .......................... :)