How to create splash screen using kotlin in android studio?

All QuestionsCategory: Mobile Apps, Android Studio, IOS, FlutterHow to create splash screen using kotlin in android studio?
chetan shidling asked 4 years ago

I need short information.

2 Answers
chetan shidling answered 4 years ago

Activitysplash.kt

package com.webianks.bluechat

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Handler
import android.os.Bundle

class ActivitySplash : AppCompatActivity() {
private var mDelayHandler: Handler? = null
private val SPLASH_DELAY: Long = 4000 //3 seconds

internal val mRunnable: Runnable = Runnable {
if (!isFinishing) {

val intent = Intent(applicationContext, MainActivity::class.java)
startActivity(intent)
finish()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
//Initialize the Handler
mDelayHandler = Handler()

//Navigate with delay
mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY)

}

public override fun onDestroy() {

if (mDelayHandler != null) {
mDelayHandler!!.removeCallbacks(mRunnable)
}

super.onDestroy()
}
}

activity_splash.xml

<?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"
tools:context=".ActivitySplash">

<ImageView
android:id="@+id/imageView"
android:layout_width="201dp"
android:layout_height="197dp"
android:layout_marginStart="105dp"
android:layout_marginTop="256dp"
android:layout_marginEnd="105dp"
android:layout_marginBottom="278dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_foreground" />

<TextView
android:id="@+id/textView"
android:layout_width="160dp"
android:layout_height="99dp"
android:layout_marginStart="125dp"
android:layout_marginTop="480dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="152dp"
android:text="CasStyles"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
chetan shidling answered 4 years ago

Activitysplash.kt

package com.webianks.bluechat

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Handler
import android.os.Bundle

class ActivitySplash : AppCompatActivity() {
private var mDelayHandler: Handler? = null
private val SPLASH_DELAY: Long = 4000 //3 seconds

internal val mRunnable: Runnable = Runnable {
if (!isFinishing) {

val intent = Intent(applicationContext, MainActivity::class.java)
startActivity(intent)
finish()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
//Initialize the Handler
mDelayHandler = Handler()

//Navigate with delay
mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY)

}

public override fun onDestroy() {

if (mDelayHandler != null) {
mDelayHandler!!.removeCallbacks(mRunnable)
}

super.onDestroy()
}
}



activity_splash.xml


<?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"
tools:context=".ActivitySplash">

<ImageView
android:id="@+id/imageView"
android:layout_width="201dp"
android:layout_height="197dp"
android:layout_marginStart="105dp"
android:layout_marginTop="256dp"
android:layout_marginEnd="105dp"
android:layout_marginBottom="278dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_foreground" />

<TextView
android:id="@+id/textView"
android:layout_width="160dp"
android:layout_height="99dp"
android:layout_marginStart="125dp"
android:layout_marginTop="480dp"
android:layout_marginEnd="126dp"
android:layout_marginBottom="152dp"
android:text="CasStyles"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>