กำหนดหน้า Splash Screen ให้แสดงข้อความที่กลางหน้าจอนาน 3 วินาที แล้วค่อยเรียกหน้า MainActivity
แก้ไขไฟล์
- strings.xml
- activity_splash.xml
- SplashActivity.kt
- AndroidManifest.xml
- Android Create Splash Screen In Kotlin
- Android Implementing Full Screen Activity Splash Screen in Kotlin Tutorial
strings.xml
<resources>
<string name="app_name">My Application</string>
<string name="loader_text">Splash Screen</string>
</resources>
กำหนดหน้า Splash Screen ให้แสงข้อความที่กลางหน้าจอ
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loader_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
หน่วงเวลา 3 วินาที ถึงค่อยเรียกหน้า MainActivity
SplashActivity.kt
package com.phaisarn.myapplication
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
class SplashActivity : AppCompatActivity() {
private var _delayHandler: Handler? = null
private val _splashDelay: Long = 3000 //3 seconds
private val _runnable: 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
_delayHandler = Handler()
//Navigate with delay
_delayHandler!!.postDelayed(_runnable, _splashDelay)
}
public override fun onDestroy() {
if (_delayHandler != null) {
_delayHandler!!.removeCallbacks(_runnable)
}
super.onDestroy()
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemUI()
}
}
private fun hideSystemUI() {
val decorView = window.decorView
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
}
บรรทัดที่ 39-55: ใช้ทำให้เป็น full screen แต่ไม่เนียน คือเห็น nav bar ก่อน แล้ว nav bar ค่อยหายไป
กำหนดให้ SplashActivity เป็นหน้าเริ่มต้น
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.phaisarn.myapplication">
<dist:module dist:instant="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
</manifest>