λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ’š 개발/μ•ˆλ“œλ‘œμ΄λ“œ

[μ•ˆλ“œλ‘œμ΄λ“œ] Splash Screen 예제

by devshin.kr 2021. 10. 20.
728x90

 

λͺ¨λ°”일 μ•±μ—μ„œ Splash Screen μ΄λž€?

- μ–΄λ–€ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μ‹€ν–‰λ˜λ©΄μ„œ κ°€μž₯ μ²˜μŒμ— λœ¨λŠ” 화면이닀.
- ν•œκ΅­μΈμ΄ λͺ¨λ‘ μ‚¬μš©ν•˜λŠ” μΉ΄μΉ΄μ˜€ν†‘μ„ 예둜 λ“€μžλ©΄, μΉ΄μΉ΄μ˜€ν†‘ μ–΄ν”Œμ„ μΌ€ λ•Œ κ°€μž₯ μ²˜μŒμ— λœ¨λŠ” λ…Έλž‘μƒ‰ 화면을 λ§ν•œλ‹€.
- 보톡 μ΄ˆκΈ°ν™” μž‘μ—…μ„ ν•˜κ±°λ‚˜ 버전 정보 ν‘œκΈ° 등에 μ‚¬μš©ν•œλ‹€.

 

이 ν™”λ©΄!!! 을 Splash Screen 이라 ν•œλ‹€.

 

 

1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity"
        android:textStyle="bold"
        android:textSize="35dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

2. activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Splash Screen"
        android:textStyle="bold"
        android:textSize="35dp"
        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. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.devshin93.splashexample">

    <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/Theme.SplashExample">
        <activity android:name=".MainActivity"> </activity>
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

4. MainActivity.java

package com.devshin93.splashexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

 

 

5. SplashActivity.java

package com.devshin93.splashexample;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity implements View.OnClickListener {
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide(); // actionBar 숨기기
        setContentView(R.layout.activity_splash);

        handler = new Handler(Looper.getMainLooper());
    }

    @Override
    protected void onStart() {
        super.onStart();

        // Splash Screen 화면이 μ‹œμž‘λ˜κ³ , λ”œλ ˆμ΄λ₯Ό μ€€ 후에 run() μ•ˆμ˜ μ½”λ“œλ₯Ό λ™μž‘ν•œλ‹€.
        // 1s = 1000ms
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                SplashActivity.this.beginMainActivity();
            }
        }, 2000);	// 2초 뒀에 run() μ•ˆμ˜ λ™μž‘μ„ 함
    }

    @Override
    public void onClick(View view) {
        // Splash Screen 화면이 클릭되면 λ°”λ‘œ MainActivity λ₯Ό μ‹€ν–‰ν•œλ‹€.
        beginMainActivity();
    }

    private void beginMainActivity() {
        // MainActivity λ₯Ό μ‹€ν–‰ν•˜λŠ” intent 생성 및 호좜
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

 

 

6. κ²°κ³Ό ν™”λ©΄

 

λŒ“κΈ€