Splash Screen in Kotlin
1: First Step
Create a project in Android Studio with include Kotlin support.
2: Create XML file splash_screen.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"><TextView
android:id="@+id/tv_splashScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Splash Screen"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"tools:layout_editor_absoluteX="148dp"tools:layout_editor_absoluteY="255dp" /></android.support.constraint.ConstraintLayout>***********************************************************************
3:Create SplashScreen.kt file
***********************************************************************
package www.abhishek.com.kotlinimport android.content.Intentimport android.os.Bundleimport android.support.v7.app.AppCompatActivity/*** Created by abhishek* */class SplashScreen : AppCompatActivity() {private val SPLASH_TIME:Long = 5000 // 5 second// inner class intialiseprivate var intentLaucher: IntentLaucher? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.splash_screen)intentLaucher = IntentLaucher() // create inner class objectintentLaucher!!.start() // call thread}/*** IntentLuncher to extends Thread and implement run method*/inner class IntentLaucher : Thread() {override fun run() {try {Thread.sleep(SPLASH_TIME)} catch (e: InterruptedException) {e.printStackTrace()}startActivity(Intent(this@SplashScreen,MainActivity::class.java))finish()}}}***********************************************************************
4: Change styles.xml to Theme.AppCompat.Light.NoActionBar
***********************************************************************
<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">//change Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light.NoActionBar<!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style></resources>***********************************************************************5: Create activity_main.xml***********************************************************************<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns: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="www.paymonk.com.kridhankotlin.MainActivity"><TextViewandroid:id="@+id/tv_splashScreen"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Main Page"android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>***********************************************************************6: Create MainActivity.kt***********************************************************************package www.abhishek.com.kotlinimport android.os.Bundleimport android.support.v7.app.AppCompatActivityimport kotlinx.android.synthetic.main.activity_main.*/*** Created by abhishek* */class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//call textview to idtv_splashScreen.setText("Replace own code")}}*********************************************************************
No comments:
Post a Comment