Introduction
Android is the most popular mobile platform available in the market. With over 85% market share worldwide, Android Operating System dominates the mobile platform market. In this article, today, I will show you how to create a splash screen of our Android app using Kotlin in Android Studio.
Requirements
Steps to be followed
Follow these steps to create a splash screen using Kotlin in Android Studio. I have included the source code in the attachment.
Step 1
Open Android Studio and start a new Android Studio Project.
Step 2
Now, add the activity and click the "Next" button.
Step 3
You can choose your application name and choose where your project is to be stored and choose the Kotlin language for coding the project. Now, select the version of Android and select the target Android devices, and click "Finish".
Step 4
Go to the drawable folder and add the image to make a splash screen.
Step 5
Go to activity_main.xml. This XML file contains the designing code for your Android app.
The XML code is given below.
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#fff"
- android:fitsSystemWindows="true">
- <androidx.appcompat.widget.AppCompatImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:src="@drawable/logo">
- </androidx.appcompat.widget.AppCompatImageView>
- </RelativeLayout>
Step 6
Go to Main Activity.kt. This Kotlin program is the backend language for your app.
The Kotlin code is given below.
- package com.abu.splashscreencsharp
- import android.content.Intent
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.os.Handler
- class MainActivity : AppCompatActivity() {
-
- private val SPLASH_TIME_OUT = 3000L
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- Handler().postDelayed(
- {
- val i = Intent(this@MainActivity, HomeActivity::class.java)
- startActivity(i)
- finish()
- }, SPLASH_TIME_OUT)
- }
- }
Step 8
Create a new empty Activity and name it as Home Activity.
The code for Home Activity XML is given below.
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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=".HomeActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Welcome to Home Screen"
- android:textSize="30sp"
- android:textStyle="bold"
- android:layout_centerInParent="true"
- android:textAllCaps="false">
- </TextView>
- </RelativeLayout>
The homeActivity.kt code is mentioned below.
- package com.abu.splashscreencsharp
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- class HomeActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_home)
- }
- }
Step 9
Then, click the "Run" button or press shift+f10 to finally run the project. And, choose the "virtual machine" option and click OK.
Conclusion