How to Create a Splash Screen in Android using Kotlin?

23 Oct, 2020
Android Splash Screen is the first screen visible to the user when the application’s launched. Splash Screen is the user’s first experience with the application that’s why it is considered to be one of the most vital screens in the application. It is used to display some information about the company logo, company name, etc. We can also add some animations to the Splash screen as well. A sample GIF is given below to get an idea about what we are going to do in this article. 
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Create another activity
Go to app > java > first package name > right-click > New > Activity > Empty Activity and create another activity and named it as SplashScreen. Edit the activity_splash_screen.xml file and add image, text in the splash screen as per the requirement. Here we are adding an image to the splash screen. Below is the code for the activity_splash_screen.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#fff"    tools:context=".SplashScreen">          <ImageView        android:id="@+id/SplashScreenImage"        android:layout_width="300dp"        android:layout_height="200dp"        android:src="@drawable/geeksforgeeks"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" />      </androidx.constraintlayout.widget.ConstraintLayout>
Go to the SplashScreen.kt file, and refer to the following code. Below is the code for the SplashScreen.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Intentimport android.os.Bundleimport android.os.Handlerimport android.view.WindowManagerimport androidx.appcompat.app.AppCompatActivity  @Suppress("DEPRECATION")class SplashScreen : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_splash_screen)                  // This is used to hide the status bar and make         // the splash screen as a full screen activity.        window.setFlags(            WindowManager.LayoutParams.FLAG_FULLSCREEN,            WindowManager.LayoutParams.FLAG_FULLSCREEN        )          // we used the postDelayed(Runnable, time) method         // to send a message with a delayed time.        Handler().postDelayed({            val intent = Intent(this, MainActivity::class.java)            startActivity(intent)            finish()        }, 3000) // 3000 is the delayed time in milliseconds.    }}
Step 3: Working with the AndroidMenifest.xml file
Go to the AndroidMenifest.xml file and add the following code in the Splash Screen Activity. This is used to hide the status bar or action bar.
android:theme=”@style/Theme.AppCompat.Light.NoActionBar”
Also, add <intent-filter> inside the Splash Screen Activity to make this activity as the starting activity. So whenever the app will execute the user can see the splash screen at the beginning. Below is the complete code for the AndroidMenifest.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.splashscreeninandroid">      <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity"></activity>        <activity            android:name=".SplashScreen"            android:theme="@style/Theme.AppCompat.Light.NoActionBar">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>  </manifest>
Step 4: Working with the activity_main.xml file
Go to the activity_main.xml file and add a text which will show “Welcome to GeeksforGeeks” when user will enter into the MainActivity. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/black"    tools:context=".MainActivity">      <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Welcome To GeeksforGeeks"        android:textColor="@color/colorAccent"        android:textSize="20dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Working with the MainActivity.kt file
Do nothing in the MainActivity.kt file as we already created a new activity for the Splash Screen. Below is the code for the MainActivity.kt file
Kotlin
import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toast  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)    }}
Find this project on Github: https://github.com/Gauravverma245/SplashScreen
android
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.