본문으로 건너뛰기

Splash Screen in Android

Splash screens are the first thing that users see when they open an app. They are used to display important information or branding messages to users while the app is loading. In Android, the Splash Screen API provides a way for developers to create a splash screen for their app.

Step 1: Create a Splash Screen Activity

To create a splash screen in Android, the first step is to create a new activity for the splash screen. This activity will be used to display the splash screen while the app is loading.

In Kotlin, you can create a new activity by extending the AppCompatActivity class and adding the @AndroidEntryPoint annotation:

@AndroidEntryPoint
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
}
}

In Java, you can create a new activity by extending the AppCompatActivity class and adding the @AndroidEntryPoint annotation:

@AndroidEntryPoint
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
}
}

Step 2: Create a Layout for the Splash Screen

After creating the activity, the next step is to create a layout for the splash screen. This layout will be used to display the splash screen while the app is loading.

In Kotlin, you can create a new layout by creating a new XML file in the res/layout directory:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/logo_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/logo" />

</RelativeLayout>

In Java, you can create a new layout by creating a new XML file in the res/layout directory:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/logo_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/logo" />

</RelativeLayout>

Step 3: Add the Splash Screen Activity to the Manifest

After creating the activity and layout, the next step is to add the splash screen activity to the Android manifest. This tells Android that the activity exists and can be launched when the app is started.

In Kotlin, you can add the activity to the manifest by adding the following code to the AndroidManifest.xml file:

<activity
android:name=".SplashActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

In Java, you can add the activity to the manifest by adding the following code to the AndroidManifest.xml file:

<activity
android:name=".SplashActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Step 4: Set a Timer for the Splash Screen

After adding the activity to the manifest, the next step is to set a timer for the splash screen. This tells Android how long the splash screen should be displayed before the app is launched.

In Kotlin, you can set a timer for the splash screen by adding the following code to the onCreate method of the SplashActivity:

Handler(Looper.getMainLooper()).postDelayed({
startActivity(Intent(this, MainActivity::class.java))
finish()
}, 3000)

In Java, you can set a timer for the splash screen by adding the following code to the onCreate method of the SplashActivity:

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}, 3000);

This sets a timer for 3 seconds before launching the main activity and finishing the splash screen activity.