Best approach to create the splash screen in android
The “best” approach has evolved significantly. The modern, recommended approach by Google is to use the SplashScreen API (Core SplashScreen library) introduced in Android 12 (API 31) and backported to all Android versions through AndroidX.
This approach ensures a consistent, performant, and flicker-free splash screen experience across the vast majority of devices.
Recommended Approach: SplashScreen Compat Library
This method provides a single, consistent API for all Android versions back to API 21 (Android 5.0).
Why it’s the best:
-
Platform Consistency: Provides the same smooth, native-like splash screen experience on old and new OS versions.
-
No Custom Activity Needed: You don’t need to create a separate
SplashActivitythat sleeps and then launches the main activity. This eliminates a potential performance bottleneck. -
Flicker-Free: The system seamlessly draws the splash screen until your app is fully drawn, preventing an ugly black or white flash.
-
Official & Maintained: It’s the standard solution provided and maintained by Google.
Step-by-Step Implementation:
1. Add the Dependency
Add the Core SplashScreen dependency to your app/build.gradle.kts (or app/build.gradle) file.
dependencies {
implementation("androidx.core:core-splashscreen:1.0.1") // Use the latest version
}
2. Create a Theme for the Splash Screen
In your themes.xml or styles.xml, create a new theme that inherits from the Theme.SplashScreen.
<resources>
<!-- Base application theme (your app's normal theme) -->
<style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar">
<!-- ... your app's colors, typography, etc. ... -->
</style>
<!-- Splash screen theme -->
<style name="Theme.MyApp.Splash" parent="Theme.SplashScreen">
<!-- This defines the background of the splash screen -->
<item name="windowSplashScreenBackground">@color/splash_screen_background</item>
<!-- This draws a circular icon (Adaptive Icon) in the center. Use @drawable for static images -->
<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_round</item>
<item name="windowSplashScreenAnimationDuration">500</item> <!-- in ms -->
<!-- The background for the status and navigation bars (optional) -->
<item name="windowSplashScreenStatusBarColor">@color/splash_status_bar_color</item>
<item name="windowSplashScreenNavigationBarColor">@color/splash_nav_bar_color</item>
</style>
</resources>
3. Apply the Splash Theme to your Launch Activity
In your AndroidManifest.xml, apply the splash theme to your main launcher activity (usually MainActivity). This is crucial.
<manifest ...>
<application ...>
<activity
android:name=".MainActivity"
android:theme="@style/Theme.MyApp.Splash" <!-- Apply splash theme HERE -->
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4. Install the Splash Screen Before super.onCreate()
In your MainActivity.kt, you must call installSplashScreen() before super.onCreate(). This allows the splash screen to remain visible until your app is ready.
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Handle the splash screen transition. MUST be called before super.onCreate().
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // Or setContent { } for Jetpack Compose
// Optional: Control how long the splash screen is visible.
// Keep the splash screen visible for this Activity until UI is ready.
splashScreen.setKeepOnScreenCondition { true } // See explanation below
// Initialize your UI (e.g., set up ViewModels, load data)
setupUI()
}
private fun setupUI() {
// Perform your longer-running operations here.
// The splash screen will now be visible until this method finishes
// and you set the condition to 'false'.
// Once your app is ready (e.g., data is loaded), you can set the condition to false.
// For a simple app, you might not need this and can remove the setKeepOnScreenCondition line.
}
}
How it works:
-
The system starts your
MainActivitywith the splash theme, which instantly draws the splash screen UI. -
installSplashScreen()tells the system to keep that splash screen visible. -
Your app’s normal UI is built behind the splash screen.
-
Once your app is fully drawn and ready (or when you tell it via
setKeepOnScreenCondition), the system seamlessly cross-fades to your app’s main UI.
I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com
