A splash screen is mostly the first screen of the app when it is opened. It is a constant screen that appears for a specific amount of time and generally shows for the first time when the app is launched. Splash screen is used to display some basic introductory information such as the company logo, content, etc just before the app loads completely.
In this tutorial, we'll implement a splash screen using the SplashScreen API, which is the modern and official way recommended by Google. This method works on Android 12+ and supports earlier versions using the support library.
Note: This Android article covered in Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note: Select Kotlin as the programming language.
Step 2: Add the Splash Screen Dependency
In your build.gradle.kts (Module :app) file, add the following dependency
dependencies {
...
implementation("androidx.core:core-splashscreen:1.0.0")
}
Step 3: Create the splash theme
Navigate to app > res > values > themes.xml and the add the following style. Create a new drawable resource file to scale the logo. Set the name as logo_inset.xml.
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Demo" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Customize your light theme here. -->
<!--<item name="colorPrimary">@color/colorPrimary</item>-->
</style>
<style name="Theme.Demo" parent="Base.Theme.Demo" />
<!-- add the following code-->
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">#D8D7D7</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/logo_inset</item>
<item name="postSplashScreenTheme">@style/Theme.Demo</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/your_logo"
android:insetLeft="40dp"
android:insetRight="40dp"
android:insetTop="40dp"
android:insetBottom="40dp" />
Step 4: Set splash theme in manifests
Navigate to app > manifests > AndroidManifest.xml and the set the default theme as the splash theme as shown below.
<application
...
android:theme="@style/Theme.App.Starting"
...>
...
</application>
Step 5: Working with MainActivity file
Navigate to app > java+kotlin > {package-name} > MainActivity.kt and make the following changes. Also navigate to activity_main.xml in res > layout folder and make the following changes.
package org.geeksforgeeks.demo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private var keepSplash = true
override fun onCreate(savedInstanceState: Bundle?) {
// Install splash screen and keep it visible while `keepSplash` is true
val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition { keepSplash }
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Start coroutine to delay splash screen removal
lifecycleScope.launch {
delay(3000) // Wait 3 seconds
keepSplash = false // Allow splash screen to be dismissed
}
}
}
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to MainActivity!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>