Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to Create a Splash Screen With Layer-List in Android?

  • Last Updated : 17 Mar, 2021

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.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

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

XML




<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item<!-- Add Background color as per your necessity-->
        <color android:color="@color/your_favoriteBackgroundColor" />
    </item>
    <item>
       <!-- Add Bitmap with gravity as center to align the content in center -->
        <bitmap 
            android:gravity="center"
            android:src="@drawable/yourDrawableResourceFile" />
    </item>
</layer-list>

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

XML




<style name="yourThemeName" parent="AppTheme">
  <item name="android:windowBackground">@drawable/yourDrawableFileName</item>
  <item name="android:windowFullscreen">true</item>
</style>

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

Kotlin




class MainActivity:AppCompatActivty(){  
    
private val mHandler = Handler(Looper.getMainLooper()) 
private val mLauncher: Launcher = Launcher()
    override fun onStart() {
        super.onStart()
        mHandler.postDelayed(mLauncher, SPLASH_DELAY.toLong())
    }
  
    override fun onStop() {
        mHandler.removeCallbacks(mLauncher)
        super.onStop()
    }
  
    private fun launch() {
        if (!isFinishing) {
               startActivity(Intent(this@MainActivity, AnotherActivity::class.java))
            finish()
        }
    }
  
  
    private inner class Launcher : Runnable {
        override fun run() {
            launch()
        }
    }
  
    companion object {
        private const val SPLASH_DELAY = 2000
        private const val TAG = "MainActivity"
    }
}

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.

XML




<activity
  android:name=".MainActivity"
  android:theme="@style/yourDrawableTheme">
    <intent-filter>
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
 



My Personal Notes arrow_drop_up
Recommended Articles
Page :