Android SplashScreen API — Modern Splash Screen for Android 12+
The core-splashscreen library provides Material Design 3 splash screens with animated transitions on Android 12+.
Setup
Add dependency:
dependencies {
implementation "androidx.core:core-splashscreen:1.0.1"
}
Theme XML Configuration
Define splash screen theme in res/values/themes.xml:
<style name="Theme.App.SplashScreen" parent="Theme.SplashScreen">
<!-- Background color -->
<item name="windowSplashScreenBackground">@color/primary</item>
<!-- Animated icon -->
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash_animated</item>
<!-- Animation duration (default 1000ms) -->
<item name="windowSplashScreenAnimationDuration">800</item>
<!-- Theme after splash exits -->
<item name="postSplashScreenTheme">@style/Theme.App</item>
</style>
Create animated vector drawable res/drawable/ic_splash_animated.xml:
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_splash">
<target
android:name="rotationGroup"
android:animation="@animator/rotation" />
</animated-vector>
Activity Setup
Install splash screen in Activity.onCreate():
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Install BEFORE super.onCreate()
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Keep splash screen visible until ViewModel ready
splashScreen.setKeepOnScreenCondition {
viewModel.isLoadingMainData.value
}
// Custom exit animation
splashScreen.setOnExitAnimationListener { splashScreenView ->
val slideOut = ObjectAnimator.ofFloat(
splashScreenView,
View.TRANSLATION_Y,
0f,
-splashScreenView.height.toFloat()
)
slideOut.duration = 300
slideOut.interpolator = DecelerateInterpolator()
slideOut.doOnEnd { splashScreenView.remove() }
slideOut.start()
}
}
}
ViewModel Initialization Flow
class MainViewModel : ViewModel() {
private val _isLoadingMainData = MutableLiveData(true)
val isLoadingMainData: LiveData<Boolean> = _isLoadingMainData
init {
viewModelScope.launch {
delay(2000) // Simulate data load
_isLoadingMainData.value = false
}
}
}
The splash screen automatically dismisses when setKeepOnScreenCondition returns false.
Top comments (0)