Create a splash screen with Count Down in the Android Kotlin Application

Posted 2 months ago
Create a splash screen with Count Down in the Android Kotlin Application

Having a strong intro is a must-have thing nowadays. Even the smallest roasting video on YouTube starts with a quick bang. Then, why we Android developers should stay back?

There is a lot of things available to create killer splash screens. Most of the StackOverflow codes are still considering people use Java to roll out their apps. But, most of the developers (like me) have already started using the latest technologies as per requirements, situations, and needs.

The latest app development technologies are really awesome, provides a lot of features and extensions to perform the most of tasks. Obviously, with great power comes great responsibility. And, right now the responsibility is to share the findings, and solutions to the occurred problems.

Yesterday, I started with a new Android application. But, having at least a splash with the logo was good to go for me. I could've done it by setting a timer.

But, but, but! I was also interested in showing some kind of count down.

Kotlin is great at this. It provides out-of-the-box a lot of features.

In this article, let's have this thing discussed over here. 

Don't forget to share with me your opinion, any problems, any advanced solutions, or almost anything related.

So, here is my project structure.

[Android Splash Screen With Kotlin.png]

Android Splash Screen With Count Down in Kotlin

It's a default project structure in Android Studio.


```
package com.factober.memecreator

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.widget.Button
import java.util.*

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

        val intent = Intent(this, HomeActivity::class.java)

        var time = 5

        val button: Button = findViewById(R.id.main_button)
        button.setOnClickListener {
            startActivity(intent)
        }

        button.text = time.toString()

        val timer = object: CountDownTimer(5000, 1000) {
            override fun onTick(p0: Long) {
                time -= 1
                Log.d("Ticked ", time.toString())
                button.text = time.toString()
            }

            override fun onFinish() {
                Log.d("Activity has started.", "")
                startActivity(intent)
                finish()
            }
        }

        timer.start()
    }
}
``` 

In this code, I've used CountDownTimer class which gave me access to the onTick function. And rest is history.

Also, if you don't want that so-called countdown thing, you can achieve your favorite splash screen with this simple implementation:


```
Timer().schedule(object : TimerTask() {
            override fun run() {
                startActivity(intent)
                finish()
            }
        }, 5000)
``` 

Give it a look and try this.

We'll create something else in the upcoming article. Till, then, bbye.