I this tutorial, I will teach you how to add Full-Screen Splash Activity in Android app using Kotlin.
Create new Activity and name SplashActivity
And mark it as LAUNCHER Activity in the Manifest. so whenever user will click on your application icon from home or from the menu this activity will be launched.
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
As you may know, that splash screens are full-screen activities in Android.
so, first of all, we have to enable full-screen mode in our Splash Activity.
Below code will hide system navigation bar and status bar.
You have to add this to your Splash Activity.
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemUI()
}
}
We are calling setSystemUiVisibilty method.
private fun hideSystemUI() {
val decorView = window.decorView
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
Drawable splashbg.png is full-screen image.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="@drawable/splashbg"
tools:context="com.es.developine.ui.splash.SplashActivity">
</android.support.constraint.ConstraintLayout>
We will be using Handler and Runnable for adding the delay in our splash activity.
And will navigate the user to MainActivity.kt after specified time interval.
class SplashActivity : AppCompatActivity() {
private var mDelayHandler: Handler? = null
private val SPLASH_DELAY: Long = 3000 //3 seconds
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemUI()
}
}
private fun hideSystemUI() {
val decorView = window.decorView
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
internal val mRunnable: Runnable = Runnable {
val intent = Intent(applicationContext, MainActivity::class.java)
startActivity(intent)
finish()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
//Initializing the Handler
mDelayHandler = Handler()
//Navigate with delay
mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY)
}
}
copyright @ http://developine.com