Learn Splash Screen using simple example written in Kotlin.

Example 1: Kotlin Android Splash Screen

A simple splash screen example in kotlin. A splash screen can be shown to users as the app is being launched. You can use that opportunity to initialize or setup your app resources, or even show your brand to your users.

Here is the demo of the created project:

Step 1: Create Project

Start by creating an empty Android Studio project.

Step 2: Dependencies

No third party dependency is needed for this project.

Step 3: Design Layout

Design your User Interface for the app using XML as shown.

(a). activity_main.xml

This will be the splash screen layout. It comprises in this case a simple imageview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/logo"/>

</LinearLayout>

(b). activity_welcome.xml

The welcome page opened after splash screen has been shown to users:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    tools:context=".WelcomeActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textSize="30sp"/>

</LinearLayout>

Step 4: Prepare Style

Use the NoActionBar as your theme parent:

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

Step 5: Write Code

We will have two files:

(a). MainActivity.kt

This will be our splash screen. We will first set it to full screen, so that it occupies the whole window. We do that as follows:

        //full screen activity code
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

Then we use Handler to delay for a specific duration then open the welcome activity:

        Handler().postDelayed(Runnable {

            //jump to welcome activity
            startActivity(Intent(this, WelcomeActivity::class.java))

            //when user back press then application is closed
            finish()

            // 3 second work splash screen application
        }, 3000)

Here’s the full code:

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.WindowManager

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //full screen activity code
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

        //handle to splash screen
        Handler().postDelayed(Runnable {

            //jump to welcome activity
            startActivity(Intent(this, WelcomeActivity::class.java))

            //when user back press then application is closed
            finish()

            // 3 second work splash screen application
        }, 3000)

    }
}

(b). WelcomeActivity.kt

Here;s the code for the welcome activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class WelcomeActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
    }
}

Run

Copy the code or download it in the link below, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author