Mastering Splash Screens in Android: A Complete Guide to Using Splash API
Introduction
The splash screen is the first impression your app makes on users. With the introduction of the Splash API in Android 12, creating a visually appealing splash screen has become more straightforward and standardised. Android 12 leverages the app icon directly for the splash screen, ensuring a consistent look and feel. This article will guide you through the process of implementing the Splash API, addressing potential issues on older Android versions, and ensuring compatibility across various devices.
Splash Screen Dimensions
The Splash API requires the splash screen icon to adhere to specific dimensions, similar to adaptive icons:
- Brand image: 160×160 dp
- App icon with an icon background: 240×240 dp (fits within a circle 160 dp in diameter)
- App icon without an icon background: 288×288 dp (fits within a circle 192 dp in diameter)
Handling Splash Screens on Lower Android Versions
On devices running Android versions lower than 12, the splash screen might appear as a blank screen like above. To address this, follow these steps:
- Add Dependency:
implementation("androidx.core:core-splashscreen:1.0.1")
2. Update Theme Style:
Add the following to your theme style and update the theme name in the. manifest file.
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<!-- Set the splash screen background, animated icon, and animation duration. -->
<item name="windowSplashScreenBackground">@color/your_prefer_color</item>
<!-- Use windowSplashScreenAnimatedIcon to add a drawable or an animated drawable. One of these is required. -->
<item name="windowSplashScreenAnimatedIcon">@drawable/your_prefer_drawable</item>
<!-- Required for animated icons. -->
<item name="windowSplashScreenAnimationDuration">200</item>
<!-- Set the theme of the Activity that directly follows your splash screen. This is required. -->
<item name="postSplashScreenTheme">@style/Theme.App</item>
</style>
3. Call `installSplashScreen()` in MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
setContentView(R.layout.activity_main)
}
}
Ensure your drawable dimensions align with the guidelines mentioned above to avoid cropping issues. For more details, refer to the Android Developer documentation.
Special Case: Larger Icons on Lower Versions
If there’s a requirement to display larger icons on lower Android versions, although not recommended, you can achieve this by creating separate drawable and values folders targeting different API levels.
- Create Folders:
drawable-v31
: Containsandroid_splash.xml
drawable
: Containsandroid_splash.xml
values-v31
: Containstheme.xml
values
: Containstheme.xml
2. Drawable File (android_splash.xml):
3. Theme for API 31+ (values-v31/theme.xml):
<style name="Theme.Splash" parent="android:Theme.Material.Light.NoActionBar" />
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/black</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/android_splash</item>
<item name="windowSplashScreenAnimationDuration">1000</item>
<item name="postSplashScreenTheme">@style/Theme.Splash</item>
</style>
4. Theme for Lower Versions (values/theme.xml):
<style name="Theme.App.Starting" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:statusBarColor">@color/black</item>
</style>
Centering the Icon in Lower Versions
One more issue you might face in lower versions is the splash icon expanding to full screen if applied directly in style. To fix this, create a drawable that wraps your preferred drawable, centering the icon with the proper size:
<layer-list xmlns:android="<http://schemas.android.com/apk/res/android>">
<item android:drawable="@color/black" />
<item
android:drawable="@drawable/android_splash"
android:gravity="center" />
</layer-list>
Conclusion
The Splash API simplifies the implementation of splash screens in Android, ensuring a consistent and professional look across different devices. By following the steps outlined above, you can address compatibility issues on lower Android versions and create a seamless user experience from the moment your app launches.
By integrating the appropriate dimensions for your splash screen icons and following best practices, you ensure that your app looks polished and performs well on all devices. Additionally, leveraging the layered drawable technique helps center your splash screen icons properly, avoiding any issues with icons expanding to full screen on older Android versions.
Here’s an example of a properly implemented splash screen on both Android 12 and lower versions:
Splash Screen on Android 12:
Splash Screen on Lower Versions:
Stay tuned for more insights and advanced concepts on using the Splash API in upcoming articles. You can also explore the complete implementation by visiting the GitHub repository. Thank you for reading!