Skip to main content

How to create a splash screen in Kotlin Android

How to create a splash screen in Kotlin Android.

Here is a detailed step-by-step tutorial on how to create a splash screen in Kotlin Android:

Step 1: Create a new Android Project

Start by creating a new Android project in Android Studio. Choose an appropriate project name and package name for your application.

Step 2: Design the Splash Screen Layout

In the res/layout folder, create a new XML file called "activity_splash.xml" to define the layout for the splash screen. Customize the layout as per your requirements. For example, you can use an ImageView to display a logo or background image.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splash_image"
android:scaleType="centerCrop" />

</RelativeLayout>

Step 3: Create a Kotlin Class for Splash Screen

In the Kotlin package of your project, create a new Kotlin class called "SplashActivity". This will be the activity that will display the splash screen.

class SplashActivity : AppCompatActivity() {

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

// Add any additional setup code here

Handler().postDelayed({
// Start the next activity after a certain delay
startActivity(Intent(this, MainActivity::class.java))
finish()
}, 2000) // Delay in milliseconds
}
}

Step 4: Modify the AndroidManifest.xml

Open the AndroidManifest.xml file and add the following code to define the SplashActivity as the launcher activity.

<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Step 5: Add the Splash Screen Image

In the res/drawable folder, add the splash image that you want to display during the splash screen. Make sure the image is named "splash_image" and has the appropriate file extension (e.g., splash_image.png).

Step 6: Build and Run the App

Build and run your Android app on an emulator or a physical device. The splash screen will be displayed for the specified delay (2000 milliseconds in this example), after which it will automatically transition to the MainActivity.

That's it! You have successfully created a splash screen in Kotlin Android. You can customize the duration of the splash screen and add any additional setup code as per your requirements.