info@androidpaper.co.in

How To Create Splash Screen In Android in kotlin

Creating a splash screen in an Android app is a great way to provide a visually appealing and branded start to your application.
A splash screen is usually displayed when the app is launched, giving it time to initialize and load any necessary resources in the background while presenting a graphical representation of your app's identity to the user.

Create a new layout file for the splash screen (activity_splash.xml):

<!-- res/layout/activity_splash.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/splashBackground">

    <!-- Add your splash screen content here, such as an app logo, title, etc. -->
    <!-- Example: -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_app_logo"
        android:contentDescription="@string/app_logo_description"/>

</RelativeLayout>

Create a new Kotlin file for the SplashActivity:

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

class SplashActivity : AppCompatActivity() {

    private val SPLASH_TIME_OUT: Long = 3000 // Splash screen timeout in milliseconds (3 seconds)

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

        // Use a Handler to delay the opening of the MainActivity
        Handler().postDelayed({
            // Start the MainActivity after the timeout
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish() // Close the splash activity so the user can't go back to it
        }, SPLASH_TIME_OUT)
    }
}

Update the AndroidManifest.xml to add the SplashActivity:

<!-- Inside the <application> tag -->
<activity
    android:name=".SplashActivity"
    android:theme="@style/Theme.AppCompat.NoActionBar"> <!-- Optionally, you can set a custom theme without the action bar for the splash screen -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<!-- Your MainActivity should already be defined in the manifest -->
<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
</activity>

Make sure to adjust the SPLASH_TIME_OUT variable in SplashActivity.kt to the desired duration of your splash screen (in milliseconds).

Customize the activity_splash.xml layout to suit your app's branding and design.
That's it! Now, when the app launches, it will display the splash screen for the specified duration and then automatically transition to the MainActivity.

Now that you have an overview of the process, you can follow the step-by-step instructions in the previous response to implement a splash screen in Android using Kotlin.