45
loading...
This website collects cookies to deliver better user experience
provides backward compatibility for the new Splash Screen APIs.
This first alpha version contains all the new APIs backported
down to API 23, with the exception of the icon background.
implementation 'androidx.core:core-splashscreen:1.0.0-alpha01'
androidx.core.splashscreen
we see two classes: SplashScreen
and SplashScreenViewProvider
. Before we turn to them, please make sure that you have setandroid {
compileSdk 31
compileSdk
, we can build and run our app just fine. To actually see a splashscreen we however need to add a few things. Here's what the docs have to say.R.style.Theme_SplashScreen
as its parent R.attr.windowSplashScreenAnimatedIcon
must be setR.attr.postSplashScreenTheme
must be set<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.JetpackSplashScreenDemo" parent="Theme.SplashScreen">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="postSplashScreenTheme">@style/Theme.MaterialComponents.DayNight.DarkActionBar</item>
<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
</style>
</resources>
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
. installSplashScreen()
. Please make sure you do so early enough (in any case prior to setContentView()
).class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
setContentView(R.layout.activity_main)
}
}
val splashScreen = installSplashScreen()
splashScreen.setOnExitAnimationListener { view ->
view.iconView.let { icon ->
val animator = ValueAnimator
.ofInt(icon.height, 0)
.setDuration(2000)
animator.addUpdateListener {
val value = it.animatedValue as Int
icon.layoutParams.width = value
icon.layoutParams.height = value
icon.requestLayout()
if (value == 0) {
setContentView(R.layout.activity_main)
}
}
val animationSet = AnimatorSet()
animationSet.interpolator = AccelerateDecelerateInterpolator()
animationSet.play(animator);
animationSet.start()
}
}
setContentView()
when the animation is complete. This may look a little hacky. Especially, as there is setKeepVisibleCondition()
. The docs say:Sets the condition to keep the splash screen visible.
The splash will stay visible until the condition isn't met
anymore. The condition is evaluated before each request to draw
the application, so it needs to be fast to avoid blocking the
UI.
false
once the animation is finished, but then I need to do a little more concurrency.