How to Create a Splash Screen With Layer-List in Android?
Splash Screen is an initial screen that gets displayed on the app’s launch. So to either display your logo or to display your app’s name on the startup of the screen. Similar to what WhatsApp or Instagram apps do on their launch. They display a simple logo of their app. Now, here comes the problem, To do this you make a launcher activity, and in that activity, to show your content you are going to use the XML. And that XML code is added in the activity class on onCreate() method’s setContentView(). So on Startup, since the onCreate() method is known to be a heavy process. The time to load the content of our XML code in the app takes too much time, till then a blank screen appears on the startup which is known as a cold start. To avoid this blank screen you can use another method that uses a layer-list and use it as a theme in the manifest.
Note: Before starting create a launcher activity and delete the XML file of that activity since we are not going to use the onCreate method in the class.
Let’s get started with the practical part.
Step 1: Create the Drawable Resource
Add a new drawable resource where you are going to add contents of the splash screen. Go to res directory -> drawable -> your_splashScreenDrawable.xml
Well in this code snippet, You are going to use item tags in the layer-list. And these Item tags have the particular attributes to satisfy your needs. So this snippet is going to be the startup screen which contains a bitmap with a background color.
Step 2: Add the drawable resource into styles.xml
And this code in styles.xml will add extra properties to the splash screen like full-screen and many more. In this the attribute android:windowBackground will add the drawable resource as a background of the screen.
Step 3: Handle the Splash Screen display time in the MainActivity.kt file
And now this code is the major part as this handles the screen display timing. Here a Launcher class extending Runnable which gets delayed by the handler object and the launcher goes to another activity when the delay time is completed. And now, The remaining crucial part is in the Manifest file of the app.
Step 4: Use the style as a theme in the Manifest file
Go to manifests Directory -> Open AndroidManifest.xml. In the Manifest file, in your activity tag add a theme attribute where you are going to add this drawable as a theme in the app.