SPLASH SCREEN WITH A LOADING ANIMATION -KOTLIN
In this article, we’ll create a splash screen with a loading animation.
The splash screen is the very first screen that we see whenever we open the app. It basically used for loading necessary data for the smooth working of the app.
CONFIGURING THE MANIFEST FILE
The first thing which we do to display the splash screen is to configure our manifest file to set the launcher on our splash screen. Put this code inside application tag.
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
SETTING UP THE LAYOUT OF THE SPLASH SCREEN
Now create the layout for the splash icon like adding the icon and name of the app along with the progress bar at the bottom.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imgLogo"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="@string/app_name"
android:textAlignment="center"
android:id="@+id/appName"
android:textColor="#ffffff"
android:textSize="20sp" />
<ProgressBar
android:id="@+id/splash_screen_progress_bar"
style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:progressTint="#6995c1"
android:layout_margin="20dp" />
</RelativeLayout
SPLASH SCREEN KOTLIN FILE
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import kotlinx.android.synthetic.main.splash_screen.*
class SplashScreen : AppCompatActivity() {
private val SPLASH_DELAY: Long = 1000 //3 seconds
private var mDelayHandler: Handler? = null
private var progressBarStatus = 0
var dummy:Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splash_screen)
mDelayHandler = Handler()
//Navigate with delay
mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY)
}
private fun launchMainActivity() {
var intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
this.finish()
mDelayHandler!!.removeCallbacks(mRunnable)
}
private val mRunnable: Runnable = Runnable {
Thread(Runnable {
while (progressBarStatus < 100) {
// performing some dummy operation
try {
dummy = dummy+25
Thread.sleep(100)
} catch (e: InterruptedException) {
e.printStackTrace()
}
// tracking progress
progressBarStatus = dummy
// Updating the progress bar
splash_screen_progress_bar.progress = progressBarStatus
}
//splash_screen_progress_bar.setProgress(10)
launchMainActivity()
}).start()
}
override fun onDestroy() {
if (mDelayHandler != null) {
mDelayHandler!!.removeCallbacks(mRunnable)
}
super.onDestroy()
}
}
In the above code, the SPLASH_DELAY variable is used to define the time interval for displaying the splash screen.
FINAL RESULT
I hope you like this article. Please do like and give your valuable suggestions in the comments section. Thank you for reading this article.