Android : Quick and Simple Splash Screen

Anubhav
2 min readMay 11, 2021
Photo by Shuvro Mojumder on Unsplash

Splash screens provide a simple initial experience (typically of the application logo) while your mobile app loads. They set the stage for your application, while allowing time for the app to initialise.

Implementing Splash Screen is pretty straightforward. I will directly proceed to the code.

Create an empty activity, and design it’s xml as per your requirements.
In my case I have a simple xml file with a TextView.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="@android:color/holo_orange_light"
tools:context=".SplashActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/splash_screen"
android:textColor="@color/black"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Then in your activity, we will write the logic for launching the main activity which we intend to launch after displaying the Splash Screen.

private const val SPLASH_SCREEN_TIME_OUT: Long = 3000

class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)

supportActionBar?.hide()
launchHomeScreen()
}

private fun launchHomeScreen() {
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
}, SPLASH_SCREEN_TIME_OUT)
}
}

I have defined constant time out value for the splash screen as 3000ms.

private const val SPLASH_SCREEN_TIME_OUT: Long = 3000

I have, also on purpose, hidden the action bar to give my Splash Screen a bigger and a better view.

supportActionBar?.hide()

Finally I have used the plain old handler to create a delay of 3 seconds, and then I launch the HomeActivity.

private fun launchHomeScreen() {
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
finish()
}, SPLASH_SCREEN_TIME_OUT)
}

Splash screen should only be displayed when the app is launched for the first time or from the killed state and not if the user presses the back button — for that I finally call the finish() method after launching the intent.

You can find the project here : https://github.com/aroranubhav/SplashScreenDemo

--

--