In the world of mobile development, first impressions are everything. When a user clicks on your application icon, he should not see a white or black screen waiting for resources to load. Splash Screen (splash screen) is a calling card that indicates that the process has begun. In the modern realities of Android, the development of this element has ceased to be a simple matter of inserting a picture into an XML file.
With the release of Android 12 and the Jetpack SplashScreen API library, the approach to implementation has changed dramatically. Now the system takes control of the launch animation, ensuring a smooth transition from the icon to the application content. Developers there is no need to reinvent the wheel, but it is important to understand the new working mechanisms. Ignoring these standards may result in your application looking outdated or working incorrectly on new devices.
In this guide, we will look in detail at how to make an Android splash screen using the latest tools. We will touch on issues of adaptation to different OS versions, theme customization, and handling complex initialization logic. Understanding these processes will allow you to create a seamless experience with your software product.
Evolution and modern launch standards
In the past, creating a loading screen was a matter of creating a separate one Activity, which displayed the logo while heavy operations were performed in the background. This method, known as "Legacy Splash Screen", often resulted in jank when transitioning to the main interface. The system did not know when the application was ready and simply killed the screensaver process on a timer or when the activity ended.
The modern approach implemented in Android 12 and available through backward compatibility for older versions works differently. The system displays a static image immediately after clicking on the icon. This image is generated based on manifest data and application resources. Once the application is ready, an animated transition to the first screen occurs.
โ ๏ธ Warning: Using outdated methods for implementing the splash screen on Android 12 and higher may result in the logo appearing twice or screens switching abruptly, which negatively affects the perceived speed of work.
The key advantage of the new API is predictability. The operating system itself manages the life cycle of the screensaver. You don't have to manually create stub activities. All that is required is the correct theme setup and use of the class InstallSplashScreen. This reduces the likelihood of errors and simplifies code maintenance.
Setting up resources and application themes
Foundation for correct display of the splash screen is the theme of the application. Unlike the old methods, where we changed the background of the window, now we set special attributes in the style. They are the ones that the system draws in the first milliseconds of launch. To do this, you need to define or modify a theme in the file. themes.xml the theme must be defined or modified.
It is important to separate resources for light and dark themes. Users appreciate it when an app respects their system settings. To do this, separate resource files are created or qualifiers are used night. The main attribute that interests us is postSplashScreenTheme. It specifies which theme to apply after the splash screen disappears.
Here is an example of how the definitions should look in XML:
<style name="Theme.App.Starting" parent="Theme.SplashScreen"><item name="windowSplashScreenBackground">@color/splash_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash</item>
- <item name="postSplashScreenTheme">@style/Theme.App</item>
</style>
Here windowSplashScreenBackground sets the background color, and windowSplashScreenAnimatedIcon the logo itself. Please note that it is recommended to use vector graphics (VectorDrawable) or adaptive icons for the icon. This ensures clear images on screens of any resolution.
Use vector graphics (XML) for the splash screen logo. This will reduce the size of the APK and provide ideal quality on screens with high pixel density.
Implementation via the Jetpack SplashScreen API
To enable the functionality, you need to add a dependency to the file build.gradle of your module. The library ensures that the new standards work even on devices running Android 6.0 and higher. Without this library, you will not be able to take advantage of the controlled disappearance of the splash screen.
After adding the dependency, let's move on to the code. The initialization logic is placed in the method onCreate of your start activity (usually this MainActivity). The method call installSplashScreen must occur before the call setContentView. Violation of the order will result in the splash screen not being displayed or displayed incorrectly.
Example implementation in Kotlin:
class MainActivity: AppCompatActivity {
override fun onCreate(savedInstanceState: savedInstanceState?) {
val splashScreen = installSplashScreen
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Further initialization logic
}
}
After installing the splash screen, you receive an object SplashScreenthat allows you to control the behavior. For example, you can set a screen hold condition. This is useful if your application needs to load large amounts of data before displaying the interface. However, this should not be abused - the user should not wait longer than 1 second after the content is ready.
โ๏ธ Checking the API implementation
Completed: 0 / 4
Adaptive icon and vector graphics
The central element of the screensaver is the icon. In modern versions of Android it should be adaptive. This means that the system can trim it or change its shape depending on the device manufacturer. To create such an icon, a resource like AdaptiveIconDrawable.
The structure of an adaptive icon consists of two layers: background and foreground is used. The background layer is usually a color or gradient, and the foreground contains the logo itself. When creating a resource in Android Studio, select Image Asset and specify the required layers. The system will automatically generate an XML file that will be used in the theme.
If you are using a static image, make sure it is centered and has sufficient padding. The logo should not touch the edges of the visible area, as the borders may be cut off on different screens. The optimal canvas size for an adaptive icon is 108x108 dp, while the logo itself should occupy about 66x66 dp in the center.
What to do if the logo is cut off?
Make sure that the indents are set correctly in the adaptive icon file (xml) (padding). For vector drawables, use the android:viewportWidth and android:viewportHeight attribute so that scaling occurs correctly.
Display time management and asynchrony
One โโof the main tasks of the developer is to synchronize the disappearance of the splash screen with the application's readiness. By default, the system keeps the screen on for a few hundred milliseconds. If your content loads faster, the splash screen will still last a minimum amount of time. If it is slower, the screen will disappear and the user will see a โleakyโ interface without data.
To solve this problem, the method setKeepOnScreenConditionis used. It accepts a lambda expression that returns a boolean value. While the condition is true, the splash screen remains on the screen. As soon as it becomes false, a transition occurs. This is an ideal mechanism for waiting for configuration loading to complete or authorization check to complete.
Consider an example with a delay to simulate loading:
splashScreen.setKeepOnScreenCondition {
// Return true while loading is in progress
isLoading.value
}
In this code isLoading is the state that changes to false after all necessary tasks have been completed in the background thread. It is important not to block the main thread (UI thread) during these operations. Use Coroutines or Executors for background work, otherwise the application may freeze along with the screensaver.
Never use artificial delays (Thread.sleep) just for the sake of the user seeing the logo. The speed of access to the application is more important than marketing.
Comparison table of implementation methods
To better understand the differences between the approaches, let's summarize the data in a table. This will help you choose the right strategy for your project, especially if you support legacy code.
Characteristic
Legacy Activity
Window Background
Jetpack API (Android 12+)
Transition smoothness
Low (jerky)
Medium
High (native)
Implementation complexity
High
Low
Medium
Time control
Full
Absent
Flexible (via conditions)
Theme support
Manual
Automatic
Automatic
The table shows that switching to Jetpack API gives the greatest gain in user experience. Despite the need to learn new methods, the results are worth it. The application looks like a native part of the operating system, which increases user confidence.
However, if you are maintaining a very old application, a complete refactoring may take time. In this case, you can use a hybrid approach, but you need to strive for the standard. Remember that Google is constantly updating the requirements for applications in the Play Store, and compliance with the Material Design guidelines becomes mandatory.
โ ๏ธ Attention: Library interfaces and Google Play requirements may change. Before publishing an updated version of the application, always check the official Android Developers documentation for new requirements for launch screens.
Frequently asked questions (FAQ)
Do I need to create a separate Activity for the splash screen on Android 12?
No, you no longer need to create a separate Activity or even recommended. The modern approach involves using one main activity and managing the splash screen through the theme and API installSplashScreen. A separate activity will only add delay and complicate navigation.
Why does the splash screen flash white before the logo appears?
This happens if the background color of the window (android:windowBackground) is different from the color specified in windowSplashScreenBackground. Make sure that the theme specified in the manifest for the start activity inherits from the correct Splash theme or has an identical background color before applying system styles.
How to animate a logo on the splash screen?
The Jetpack library supports animated vector drawables (AnimatedVectorDrawable). You can assign such a resource to the windowSplashScreenAnimatedIconattribute. However, remember that the animation should be short and unobtrusive so as not to irritate the user every time you launch it.
Does this method work on Android 7 and 8?
Yes, the Jetpack SplashScreen library is backwards compatible. On older versions of Android, the behavior will be emulated using available system tools, providing consistent code across all OS versions. The only difference can be in the smoothness of the transitions.
Is it possible to completely disable the splash screen?
Technically, it is possible by making the background transparent or removing the icon, but this is bad practice. The user should see a visual response to his action (clicking an icon). The absence of a splash screen creates the feeling that the application โfreezesโ before the interface appears.