Splash Screen Tutorial using Android Studio

Splash Screen is the first thing you see when you open an application. It makes your application feel pretty professional if you do it right. But it does not really contribute to what the application does. But, sometimes you might want to do some API calls before your application is ready to use. This is what the Splash Screen is mostly used for both decorative and functional purposes. In either case let us see how to create a splash screen using Android Studio. I will show you both Kotlin and Java code to create a simple splash screen.

Quick Navigation

Setting the activity as launcher screen

Before we dive into any code we need to set one Activity for our splash screen as LAUNCHER. Let us see how we can do that.

Open AndroidManifest file which is an XML. And You will see something like this in the file.

android splash screen launcher filter

Include the following block to the activity you want to make the splash screen. Which in my case is the SplashActivity.

Let us move onto the code to see how we can launch our PostSplashActivity after X seconds.

Also Read : Learn How To Create Tabs With ViewPager2


Extracting the startActivity() code

I have created a method launchPostSplashActivity() to launch the PostSplashActivity. It doesn’t really contain anything more than this

private fun launchPostSplashActivity() { // launch another activity val intent = Intent(this@SplashActivity, PostSplashActivity::class.java) startActivity(intent) finish() // necessary because we do not want user to come back to this activity }
Code language: Kotlin (kotlin)

And if you are using Java.

private void launchPostSplashActivity() { // launch another activity Intent intent = new Intent(JavaSplashActivity.this, PostSplashActivity.class); startActivity(intent); finish(); // necessary because we do not want user to come back to this activity }
Code language: Java (java)

And obviously enough this method is inside the SplashActivity.

Using the Timer with TimerTask

This might be the most easiest and quick solution to our problem let us see how.

Let us create a method first named withTimerTask(). Which will look something like this in Kotlin.

private fun withTimerTask(delay: Long) { val timerTask = object : TimerTask() { override fun run() { launchPostSplashActivity() } } val timer = Timer("SplashScreenTimer") timer.schedule(timerTask, delay) }
Code language: Kotlin (kotlin)

If you are not familiar with Kotlin. Look at the same code in Java below.

private void withTimerTask(long delay) { TimerTask timerTask = new TimerTask() { @Override public void run() { launchPostSplashActivity(); } }; Timer timer = new Timer(); timer.schedule(timerTask, delay); }
Code language: Java (java)

You will invoke this method in your onCreate() method by passing a delay time in milliseconds. Like this withTimerTask(3_000L), yes you can separate numbers in Java and Kotlin with a _ and L is just for indicating Long literal.

Also Read : Consume an API with Retrofit in Android


Using Handler with Runnable

IF you don’t like the use of Timer to solve this problem we have another way to do this. And that is using Handler with Runnable.

See the code below to better understand how it works.

private fun withHandlerRunnable(delay: Long) { val runnable = Runnable { launchPostSplashActivity() } val handler = Handler(Looper.getMainLooper()) handler.postDelayed(runnable, delay) }
Code language: Kotlin (kotlin)

And if you want Java version of the code

private void withHandlerRunnable(Long delay) { Runnable runnable = new Runnable() { @Override public void run() { launchPostSplashActivity(); } }; Handler handler = new Handler(); handler.postDelayed(runnable, delay); }
Code language: Java (java)

Just like above invoke this method by passing the delay in milliseconds as withHandlerRunnable(3_000L).

Using coroutine

We can use coroutine to accomplish this in Kotlin.

Want to learn more about Kotlin read: Kotlin Coroutine in Android : Understanding the Basics.

First you have to add the dependency for coroutine.

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
Code language: JavaScript (javascript)

Then we have created this method withCoroutine() as below.

private fun withCoroutine(delay: Long) { mScope.launch { delay(delay) withContext(Dispatchers.Main) { launchPostSplashActivity() mScope.cancel(null) } } }
Code language: Kotlin (kotlin)

You will see that we have used mScope variable which is declared in the SplashActivity.

private lateinit var mScope: CoroutineScope
Code language: Kotlin (kotlin)

And this is initialized just before we invoke the method in onCreate() as

mScope = CoroutineScope(Dispatchers.IO) withCoroutine(3_000L)
Code language: Kotlin (kotlin)

And the only downside of using coroutine is that it’s not supported with Java. So for Java you only have 2 ways.

You can also use Thread but for a simple task using Thread is an overkill. You might want to do that for asynchronous processing.

That was a simple explanation of creating Splash Screen using Android Studio.

Also Read : Async Task in Android is Deprecated: There are Better Ways


Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *