Here in this tutorial i am going to explain you about creating a splash screen in Kotlin language. A splash screen is the first launching activity of the applications which waits for certain seconds and go to the next page either to login page or home page of the application. Splash screen is common to most of the mobile applications . It is very simple to create a splash screen using a Handler class.
If you are using Android Studio 3.0 or above then please start creating the project as Kotlin Project. If you are using an older version of Android Studio IDE then please add Kotlin plugin from JetBrains and configure Kotlin into the project.
Step 1: Create A Layout XML file
Create a layout res/layout/activity_splash.xml
<FrameLayout 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="#ffffff" tools:context="devdeeds.com.kotlinsplashscreen.SplashActivity"> <!-- The primary full-screen view. This can be replaced with whatever view is needed to present your content, e.g. VideoView, SurfaceView, TextureView, etc. --> <TextView android:id="@+id/fullscreen_content" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:keepScreenOn="true" android:text="@string/loader_text" android:textColor="#000" android:textSize="25sp" /> </FrameLayout>
Step 2: Create SplashActivity Class
SplashActivity.kt
import android.content.Intent import android.os.Bundle import android.os.Handler import android.support.v7.app.AppCompatActivity /** * A sample splash screen created by devdeeds.com * by Jayakrishnan P.M */ class SplashActivity : AppCompatActivity() { private var mDelayHandler: Handler? = null private val SPLASH_DELAY: Long = 3000 //3 seconds internal val mRunnable: Runnable = Runnable { if (!isFinishing) { val intent = Intent(applicationContext, HomeActivity::class.java) startActivity(intent) finish() } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) //Initialize the Handler mDelayHandler = Handler() //Navigate with delay mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY) } public override fun onDestroy() { if (mDelayHandler != null) { mDelayHandler!!.removeCallbacks(mRunnable) } super.onDestroy() } }
Add Splash Activity As The Launch Page In Manifest
res/layout/activity_splash.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="devdeeds.com.kotlinsplashscreen"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".SplashActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/app_name" android:theme="@style/FullscreenTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".HomeActivity" /> </application> </manifest>