January 31, 2022

Splash Screen in Kotlin

          On this blog I am going to explain how to show a splash screen using Handler. You can find more about Handler from Here.  On this I am going to show the redirection to new Activity page after the delay I set.  Redirection will be from SplashActivity which is my splash screen to MainActivity which is my landing page

Here I am using a delay of 4000 MS (4 Seconds)


Here I am using  postDelayed function for this,

fun postDelayed(r: Runnable!, delayMillis: Long): Boolean


Sample Code


import android.content.Intent
import android.os.Bundle
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import com.example.MainActivity
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)

Handler().postDelayed({

val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
        }, 4000)

}
}