First-ever Splash screen API in android recommended by Google

Sachin Rajput
Native Mobile Bits
Published in
2 min readFeb 27, 2022
official API

Finally, Android12 gives us the first-ever official Splash Screen API🥰 This new recommended Splash Screen API is just awesome, and it makes implementing Splash very easy with just a few lines of code.

Let's quickly cover the implementation in the next few steps:

Step1.) Update SDK to API

Inside your project’s build.gradle file

android {
compileSdk 31

defaultConfig {
applicationId "com.sachin.android12migrations"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"

}
}

Step2.) Add Splash Screen API dependency inside your project

dependencies {


implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.4.0'


//Splash screen Api
implementation 'androidx.core:core-splashscreen:1.0.0-beta01'



}

and now sync the project.

Step3.) Define a splash theme inside your styles.xml or inside your themes.xml

<style name="SplashScreenTheme" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/teal_200</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_app_logo</item>
<item name="postSplashScreenTheme">@style/AppTheme</item>
</style>

Step4.) Use this theme inside the project’s launcher activity in the manifest.

<activity
android:name=".views.MainActivity"
android:exported="true"
android:theme="@style/SplashScreenTheme">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Step5.) Inside this launcher-activity use, this new splash’s extension

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
//need to add this
installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

}

and that’s it we can run the app and it will show a nice splash by the themes which we added no more dedicated activities needed. You can check the actual implementation with a complete project migration here

Bonus: in case you want to add some delay to keep showing a splash screen for some time, we can use viewTreeObserver like this:

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val mainView = findViewById<View>(android.R.id.content)
mainView.viewTreeObserver.addOnPreDrawListener(
object : ViewTreeObserver.OnPreDrawListener{
override fun onPreDraw(): Boolean {
//maybe checking locale or making some api to fetch some details
if(splashViewModel.isDataLoaded.value == true){
mainView.viewTreeObserver.removeOnPreDrawListener(this)
}
return false
}

}
)

}

Thanks for staying till the end here with me, Hope I was able to share some valuable information with you!! You can also check this Migration journey where I migrate this project in the video on our youtube channel Native Mobile Bits

Full Source Code Link for the same project used here in this migration:

Do hit the 👏 to show some ❤️

See you next time :) till then “be you be happy + happy coding”

For more of our latest learnings on some interesting Android&iOS topics, you can connect to us on our youtube channel Native Mobile Bits or LinkedIn & Github.

--

--