How To Create Splash Screen Using Kotlin In Android Studio

Introduction

 
Android is the most popular mobile platform available in the market. With over 85% market share worldwide, Android Operating System dominates the mobile platform market. In this article, today, I will show you how to create a splash screen of our Android app using Kotlin in Android Studio.
 
Requirements

Steps to be followed

 
Follow these steps to create a splash screen using Kotlin in Android Studio. I have included the source code in the attachment.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
How We Can Create Splash Screen Using Kotlin In Android Studio
 
Step 2
 
Now, add the activity and click the "Next" button.
 
How We Can Create Splash Screen Using Kotlin In Android Studio
 
Step 3
 
You can choose your application name and choose where your project is to be stored and choose the  Kotlin language for coding the project.  Now, select the version of Android and select the target Android devices, and click "Finish".
 
How We Can Create Splash Screen Using Kotlin In Android Studio
 
Step 4
 
Go to the drawable folder and add the image to make a splash screen.
 
How We Can Create Splash Screen Using Kotlin In Android Studio
 
Step 5
 
Go to activity_main.xml. This XML file contains the designing code for your Android app.
 
How We Can Create Splash Screen Using Kotlin In Android Studio
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>          
  2. <RelativeLayout          
  3.    xmlns:android="http://schemas.android.com/apk/res/android"          
  4.    xmlns:tools="http://schemas.android.com/tools"          
  5.    android:layout_width="match_parent"          
  6.    android:layout_height="match_parent"          
  7.    android:background="#fff"          
  8.    android:fitsSystemWindows="true">         
  9. <androidx.appcompat.widget.AppCompatImageView          
  10.    android:layout_width="wrap_content"          
  11.    android:layout_height="wrap_content"          
  12.    android:layout_centerInParent="true"         
  13.    android:src="@drawable/logo">          
  14. </androidx.appcompat.widget.AppCompatImageView>          
  15. </RelativeLayout>   
Step 6
 
Go to Main Activity.kt. This Kotlin program is the backend language for your app.
 
How We Can Create Splash Screen Using Kotlin In Android Studio
 
The Kotlin code is given below.
  1. package com.abu.splashscreencsharp      
  2. import android.content.Intent      
  3. import androidx.appcompat.app.AppCompatActivity      
  4. import android.os.Bundle      
  5. import android.os.Handler      
  6. class MainActivity : AppCompatActivity() {      
  7.    // Splash screen timer      
  8.    private val SPLASH_TIME_OUT = 3000L      
  9.    override fun onCreate(savedInstanceState: Bundle?) {      
  10.       super.onCreate(savedInstanceState)      
  11.       setContentView(R.layout.activity_main)      
  12.       Handler().postDelayed(      
  13.       {      
  14.          val i = Intent(this@MainActivity, HomeActivity::class.java)      
  15.          startActivity(i)      
  16.          finish()      
  17.       }, SPLASH_TIME_OUT)      
  18.    }    
  19. }  
Step 8
 
Create a new empty Activity and name it as Home Activity.
 
The code for Home Activity XML is given below.
  1. <?xml version="1.0" encoding="utf-8"?>      
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      
  3.    xmlns:app="http://schemas.android.com/apk/res-auto"      
  4.    xmlns:tools="http://schemas.android.com/tools"      
  5.    android:layout_width="match_parent"      
  6.    android:layout_height="match_parent"      
  7.    tools:context=".HomeActivity">      
  8. <TextView      
  9.    android:layout_width="wrap_content"      
  10.    android:layout_height="wrap_content"      
  11.    android:text="Welcome to Home Screen"      
  12.    android:textSize="30sp"      
  13.    android:textStyle="bold"      
  14.    android:layout_centerInParent="true"      
  15.    android:textAllCaps="false">      
  16. </TextView>      
  17. </RelativeLayout>  
The homeActivity.kt code is mentioned below.
  1. package com.abu.splashscreencsharp      
  2. import androidx.appcompat.app.AppCompatActivity      
  3. import android.os.Bundle      
  4. class HomeActivity : AppCompatActivity() {      
  5.    override fun onCreate(savedInstanceState: Bundle?) {    
  6.       super.onCreate(savedInstanceState)    
  7.       setContentView(R.layout.activity_home)    
  8.    }    
  9. }    
Step 9
 
Then, click the "Run" button or press shift+f10 to finally run the project. And, choose the "virtual machine" option and click OK.
 

Conclusion

 
How We Can Create Splash Screen Using Kotlin In Android Studio
 
How We Can Create Splash Screen Using Kotlin In Android Studio