What is Splash Screen? How to Implement Splash Screen for Android App in Kotlin?
Splash Screen is a graphical control element consisting of a window containing an image, logo, and a current version of the application. It's the very first chance of creating a positive impact on the users. It appears while the app is launched the app. Many times the Splash screen is called a launch screen.
For example: These are the Splash Screen of WhatsApp and Instagram
How to Implement Splash Screen for Android App in Kotlin?
To create a splash screen in Kotlin follow the below steps.
Step 1: Add Dependency in Gradle
The core Splash Screen library brings the Android 12 splash screen to all devices from API 23. For this add following dependency to your project
Add the following dependency to your app's
build.gradle
file:implementation "androidx.core:core-splashscreen:1.0.0"
Step 2: Design the Splash Screen
Add images or logos to your
res/drawable
folder and define colors in theres/values/colors.xml
file.Step 3: Create Splash Screen Theme
Define a theme for your splash screen in your
res/values/styles.xml
file:Use "windowSplashScreenBackground" to fill the background with a specific single color
Use "windowSplashScreenAnimatedIcon" to replace the icon in the center of the starting window
Use "postSplashScreenTheme" to redirect Theme for activity.
<style name="Theme.App.SplashScreen" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/blue</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/notify</item>
<!-- Add other splash screen attributes if needed -->
</style>
Step 4: Update Activity Theme in Manifest
Update your main activity theme in the AndroidManifest.xml
file:
<activity
android:name=".MainActivity"
android:theme="@style/Theme.App.SplashScreen"
android:exported="true">
<!-- Add other activity attributes if needed -->
</activity>
Step 5: Implement Splash Screen in MainActivity
In your MainActivity.kt
, use the installSplashScreen()
method to set up the splash screen. You can also add a delay to simulate the splash screen duration:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Simulate a delay for the splash screen
Thread.sleep(3000)
// Install the splash screen
installSplashScreen()
// Set the content view for your main activity
setContentView(R.layout.activity_main)
}
}
Note: It's essential to be cautious with using Thread.sleep()
as it may lead to ANR (Application Not Responding) issues. In a real application, you would want to perform any necessary initialization or background tasks asynchronously.
Conclusion
In conclusion, implementing a splash screen in your Android app using Kotlin involves a series of straightforward steps. By incorporating the core-splashscreen
library and customizing the theme, visuals, and duration, you can create an engaging and visually appealing launch experience for your users. Remember to consider the overall design principles of your app and tailor the splash screen accordingly.
Additionally, for more detailed information and access to the complete source code, you can refer to the GitHub repository here. Exploring the repository will provide you with a deeper understanding of the implementation and allow you to customize the splash screen to suit the unique branding and design requirements of your application.