DEV Community

myougaTheAxo
myougaTheAxo

Posted on

Android SplashScreen API — Modern Splash Screen for Android 12+

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"
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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()
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The splash screen automatically dismisses when setKeepOnScreenCondition returns false.

8 Android app templates: https://myougatheax.gumroad.com

Top comments (0)