In this article, we will see how to create a Splash Screen in Android Studio using Kotlin Language.
What is Splash Screen?
A splash screen is a graphical control element that consists of a window with an image, a logo, or any widget. While a game or any app is being launched, a splash screen may appear. A splash screen is a screen that contains the introduction an app or website.
Implementation:
Create a new Project in android studio.
Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish
|
Go to activity_splash.xml file and add the following code
<LinearLayout 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:orientation="vertical"
android:gravity="center"
android:background="#ECC8C8"
tools:context=".SplashActivity">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/android2"
android:scaleType="centerCrop">
</ImageView>
</LinearLayout>
|
Open SplashActivity.kt file and hide the action bar or status bar line by adding the following code
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
supportActionBar?.hide()
|
this code will help you to make the splash activity fullscreen.
Add Handler method to the SplashActivity.kt file for persist the screen for particular time (Add below setContentView(R.layout.activity_splash)).
Handler().postDelayed({
startActivity(Intent(this, HomeActivity::class.java))
finish()
}, 2500)
|
Create a new empty activity name HomeActivity,kt file
app > java > package name > right-click > New > Activity /Empty Activity > enter name(HomeActivity.kt) > Finish.
|
Open activity_home.xml file and add following code.
<LinearLayout 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"
android:gravity="center"
android:background="#ECC8C8"
tools:context=".HomeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Home"
android:textStyle="bold"
android:textSize="29sp"
android:textColor="@color/white"
></TextView>
</LinearLayout>
|
Now run the code in your emulator or device, you will get the following output.
Complete Source code of Splash Screen Example:
activity_splash.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center"
android:background="#ECC8C8"
tools:context=".SplashActivity">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/android2"
android:scaleType="centerCrop">
</ImageView>
</LinearLayout>
|
SplashActivity.kt file
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.Window
import android.view.WindowManager
import com.nishajain.kotinexamples.R
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
supportActionBar?.hide()
setContentView(R.layout.activity_splash)
Handler().postDelayed({
startActivity(Intent(this, HomeActivity::class.java))
finish()
}, 2500)
}
}
|
activity_home.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:background="#ECC8C8"
tools:context=".SplashScreen.HomeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Home"
android:textStyle="bold"
android:textSize="29sp"
android:textColor="@color/white"
></TextView>
</LinearLayout>
|
Conclusion: We have covered how to design and implement splash screen in Android using kotlin language.