LoginSignup
1
0

More than 1 year has passed since last update.

[Android][Kotlin] Splash画面の実装 コピペ可能

Last updated at Posted at 2022-12-21

コピペでいけます!

アプリ起動時にいきなりアプリ画面を表示させるのではなく、LINEみたいに一旦起動中の画面を表示させたいですよね。
そんな時に使えるSplash画面。
コピペでいけるので、ぜひ試してください。
方法は超簡単です!

まずは

build.gradle
dependencies {
    // コルーチン設定
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
}
AndroidManifest.xml
// メインのActivityをSplashに
<activity
    android:name=".SplashActivity"/>
・
・
・
// メイン画面に遷移できるように追記
<activity
    android:name=".MainActivity"/>

ソースコード

SplashActivity.kt
class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash) // レイアウトは各自で

        CoroutineScope(Dispatchers.Main).launch {
            // 一秒間表示 (二秒間なら2000)
            delay(1000)
            // MainActivityへ画面遷移
            val intent = Intent(this@SplashActivity, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
    }
}

レイアウトに

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

をつけると、通信時によくある「ぐるぐる」を表示できます!!
これで、っぽくなるのでおすすめです!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0