어플에 시작화면을 해볼 것이다. 나는 이미지 작업이 안되니 간단하게 해볼 것이다. 

안드로이드 12 에서 Splash screen(windowSplashScreenBackground)를 해볼 예정이다.

 

Splash Activity를 Delay 하는 방식이다. (Delay를 하지않아도 Splash가 가능하다.)

 

1. Splash로 사용할 이미지를 drawable 폴더에 추가한다.


2. Splash , Main 화면을 만든다.

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="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

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=".SplashActivity">

</androidx.constraintlayout.widget.ConstraintLayout>


3. 화면을 추가했으니 manifest에 수정해줘야한다.

AndroidManifest.xml

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

    <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"
            android:theme="@style/SplashTheme"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"
            android:exported="true">
        </activity>
    </application>
</manifest>

4. 스타일에 테마를 추가한다.

style.xml

<resources>
... 
    <style name="SplashTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowBackground">@drawable/xxx</item> //xxx <= drawable 폴더에 추가한 이미지 이름
    </style>
</resources>

5. java 파일에 delay 코드만 추가하면 된다.

SplashActvity.java

package com.saii.splash;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        gotoMain(1);  // 파라미터 만큼 delay가 된다.

    }

    private void gotoMain(int sec) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 1000 * sec);
    }
}

 

 


참조

https://github.com/saii42/android/tree/main/example/Splash

 

GitHub - saii42/android: android

android. Contribute to saii42/android development by creating an account on GitHub.

github.com

https://developer.android.com/guide/topics/ui/splash-screen#java

 

Splash screens  |  Android Developers

Splash screens Important: If you have previously implemented a custom splash screen in Android 11 or lower, you'll need to migrate your app to the SplashScreen API to ensure that it displays correctly in Android 12 and higher. For instructions, see Migrate

developer.android.com

 

+ Recent posts