흔히 어플리케이션이 켜지기 전 나오는 화면을 Splash Screen, Launch Screen이라고 부른다.
스플래시 화면은 단순히 보여주기 용도로 넣는 경우도 있지만, 주로 앱에서 필요한 리소스들을 다운받을 때 대기 화면 용도로도 사용한다.
스플래쉬 액티비티 화면
스플래쉬 액티비티를 새로 생성해줄 것이다. >> SplashActivity.kt
위 액티비티를 첫 화면에 띄우기 위해 AndroidManifest.xml 에 다음과 같이 수정해준다.
<activity
android:name=".SplashActivity"
android:exported="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
화면 만들기
이대로 끝낸다면 액티비티에 타이틀, 액션바가 같이 나오게된다.
타이틀 액션바 없애기
생성한 액티비티에 타이틀과 액션바를 없애보자.
먼저 res > values > themes > themes.xml 에 다음을 추가한다.
<style name="SplashTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
그리고 다시 AndroidManifest.xml 에 작성했던 부분에
android:theme="@style/SplashTheme"
를 추가해준다.
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme"
android:exported="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2초 동안 화면에 띄우기
2초동안 화면을 보여주고 메인화면으로 넘어가기 위한 코드를 짜보자
@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
moveMainActivity(2);
}
private fun moveMainActivity(sec: Int) {
Handler(Looper.getMainLooper()).postDelayed({
val intent = Intent(this, MainActivityKt::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
startActivity(intent)
finish()
}, (1000 * sec).toLong())
}
}
결과