Implementing Splash Screen
1. Setting Up Splash Screen API
| Property | Description | Values |
|---|---|---|
| android:name | Component name for the splash screen activity | androidx.core.splashscreen.SplashScreen |
| android:theme | Theme to apply to the splash screen | ?android:attr/windowSplashScreenBackground |
Example: Splash Screen Setup in AndroidManifest.xml
<activity
android:name="com.example.SplashActivity"
android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Example: Kotlin Activity Implementation
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition {
// Keep splash screen until content is loaded
false
}
}
}
2. Configuring Splash Theme
| Property | Value | Description |
|---|---|---|
| windowSplashScreenBackground | "#FFFFFF" |
Background color of the splash screen |
| windowSplashScreenIconSize | 128dp |
Size of the splash screen icon |
| windowSplashScreenAnimatedIcon | "@drawable/splash_icon" |
Animated icon to display during splash |
| windowSplashScreenAnimationDuration | 300 |
Duration of splash screen animation in milliseconds |
Example: Define splash theme in res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/splash_background</item>
<item name="windowSplashScreenIconSize">128dp</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/splash_icon</item>
<item name="windowSplashScreenAnimationDuration">300</item>
</style>
</resources>
Example: Apply theme in AndroidManifest.xml
<activity
android:name=".MainActivity"
android:theme="@style/Theme.App.Starting"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3. Installing Splash Screen
| Property | Description | Values |
|---|---|---|
| implementation | Declares the dependency for splash screen support | androidx.core:core-splashscreen:1.0.1 |
Note: Ensure the
core-splashscreen library is added to your app's build.gradle file.
Warning: The splash screen API requires Android API level 21 or higher.
4. Customizing Animation
| Property | Description | Values |
|---|---|---|
| android:windowSplashScreenAnimationDuration | Duration of the splash screen animation in milliseconds | integer |
| android:windowSplashScreenIconSize | Size of the splash screen icon in pixels | dimension |
| android:windowSplashScreenBackground | Background color or drawable for the splash screen | color or drawable |
Example: Customizing Animation Duration
<style name="SplashTheme" parent="Theme.SplashScreen">
<item name="windowSplashScreenAnimationDuration">1000</item>
</style>
Example: Setting Background Color
<style name="SplashTheme" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/splash_background</item>
</style>
Note: The animation duration must be a positive integer value in milliseconds.
5. Handling Branding
| Property | Description | Values |
|---|---|---|
| android:windowSplashScreenBackground | Defines the background color or drawable for the splash screen | #FFFFFF (color) or @drawable/splash_background (drawable) |
| android:windowSplashScreenAnimatedIcon | Specifies the animated icon to display during splash screen | @drawable/animated_logo |
| android:windowSplashScreenIconSize | Sets the size of the splash screen icon in pixels | 100dp |
| android:windowSplashScreenAnimationDuration | Defines the duration of the splash screen animation in milliseconds | 500 |
Example: Define Splash Screen in Theme
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/splash_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/animated_logo</item>
<item name="windowSplashScreenIconSize">100dp</item>
<item name="windowSplashScreenAnimationDuration">500</item>
</style>
Note: The splash screen must be defined in a theme that extends
Theme.SplashScreen.
Warning: Ensure that the animated icon is compatible with all target Android versions.
6. Managing Exit Animation
| Property | Description | Values |
|---|---|---|
| android:windowExitAnimation | Defines the animation played when the splash screen exits | @anim/exit_fade_out or custom animation resource |
Example: Define Exit Animation
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
Example: Apply Exit Animation in Theme
<style name="SplashTheme" parent="Theme.SplashScreen">
<item name="android:windowExitAnimation">@anim/exit_fade_out</item>
</style>
7. Supporting Older Versions
| Feature | Description | Support Status |
|---|---|---|
| SplashScreen API | Introduced in Android 12 (API level 31) | NEW |
| Legacy Splash Screen | Use androidx.core.splashscreen.SplashScreen for older versions |
DEPRECATED |
| Backward Compatibility | Supports API levels 21 and above | EXPERIMENTAL |
Example: Implementing Splash Screen for API 21+
import androidx.core.splashscreen.SplashScreen
import androidx.core.splashscreen.SplashScreenViewProvider
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = SplashScreen.install(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
splashScreen.setKeepOnScreenCondition {
// Keep splash screen until content is loaded
false
}
}
}
Note: For devices running Android 12 or higher, use the new
SplashScreen API directly. For older versions, ensure you're using the appropriate androidx.core.splashscreen library.
Warning: Do not mix the new and legacy splash screen APIs in the same app, as it may lead to inconsistent behavior or crashes.
8. Implementing Loading Logic
Example: Basic Loading Logic with Coroutines
import kotlinx.coroutines.*
class SplashScreenViewModel : ViewModel() {
private val _isLoading = MutableLiveData<Boolean>()
val isLoading: LiveData<Boolean> = _isLoading
fun loadAppData() {
viewModelScope.launch {
_isLoading.value = true
delay(2000) // Simulate loading
// Fetch data from API or DB here
_isLoading.value = false
}
}
}
| Property | Description | Default |
|---|---|---|
| isLoading | Indicates whether the app is loading data | false |
| viewModelScope | Coroutine scope tied to the ViewModel lifecycle | Required |
Note: Use
viewModelScope to ensure coroutines are cancelled when the ViewModel is cleared.Warning: Avoid blocking the main thread during loading logic.
Loading Logic Best Practices
- Use
viewModelScopefor lifecycle-aware coroutines - Simulate loading with
delay()in development - Fetch real data from network or database in production
- Update UI state with
LiveDataorStateFlow
9. Using Animated Icons
| Property | Description | Values | Notes |
|---|---|---|---|
| android:src | Icon resource to display | @drawable/animated_icon |
Must be a valid drawable resource |
| android:tint | Color to tint the icon | ?attr/colorOnPrimary |
Applies to all frames of the animation |
| app:showAnimation | Animation to show the icon | fade_in, slide_in |
Custom or system animations supported |
Example: Animated Icon in Splash Screen
<ImageView
android:id="@+id/splashIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animated_logo"
android:tint="?attr/colorOnPrimary"
app:showAnimation="fade_in"
android:layout_centerInParent="true" />
Note: Animated icons must be defined in
res/drawable using <animated-vector> or <animation-list>.
Warning: Ensure the animated icon does not exceed 200KB to maintain splash screen performance.
10. Testing Splash Screen
Example: Unit Test for Splash Screen Logic
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
@RunWith(AndroidJUnit4::class)
class SplashScreenTest {
@Test
fun testSplashScreenNavigation() {
val splashViewModel = Mockito.mock(SplashViewModel::class.java)
Mockito.`when`(splashViewModel.shouldNavigateToHome()).thenReturn(true)
// Simulate splash screen logic
val result = splashViewModel.shouldNavigateToHome()
assert(result)
}
}
| Test Type | Description | Tools |
|---|---|---|
| Unit Test | Tests logic in isolation without Android framework | JUnit, Mockito |
| Instrumented Test | Tests actual UI and lifecycle behavior on device/emulator | Espresso, JUnit |