Friday 30 March 2018

Splash Screen in Kotlin language,Splash Screen in Kotlin Android,Splash Screen in Kotlin




                            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.kotlin
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
/**
* Created by abhishek
* */
class SplashScreen : AppCompatActivity() {
private val SPLASH_TIME:Long = 5000 // 5 second
    // inner class intialise
private var intentLaucher: IntentLaucher? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splash_screen)
intentLaucher = IntentLaucher() // create inner class object
intentLaucher!!.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.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="www.paymonk.com.kridhankotlin.MainActivity">
<TextView
android: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.kotlin
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import 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 id
tv_splashScreen.setText("Replace own code")
}
}
*********************************************************************

No comments:

Post a Comment