The "splash screen" in Android is the initial screen that appears when you launch an app.
It serves as an introduction or branding screen before the main content loads. To implement a splash screen handler in Android, you'll typically follow these steps using XML and Kotlin:
XML Syntax (layout/splash_screen.xml):
Create a layout file for the splash screen:
<!-- splash_screen.xml --> <RelativeLayout xmlns:android="<http://schemas.android.com/apk/res/android>" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/splash_background"> <!-- Add your logo or any branding elements here --> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/app_logo" android:layout_centerInParent="true" /> </RelativeLayout>
Kotlin Syntax (SplashScreenActivity.kt):
Create a dedicated activity to handle the splash screen:
import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class SplashScreenActivity : AppCompatActivity() { private val SPLASH_TIME_OUT = 3000 // Time in milliseconds override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.splash_screen) // Handler to navigate to the next screen after SPLASH_TIME_OUT val splashHandler = object : Runnable { override fun run() { // Define your main activity and navigate to it val intent = Intent(this@SplashScreenActivity, MainActivity::class.java) startActivity(intent) finish() // Close the splash screen activity } } // Start the handler after a delay val splashHandlerThread = Thread(splashHandler) splashHandlerThread.start() } }
Example:
- Create a layout file for your splash screen (
splash_screen.xml).
- Create a new activity (
SplashScreenActivity.kt) to handle the splash screen logic.
- Set the
SplashScreenActivityas your launcher activity in the AndroidManifest.xml:
<activity android:name=".SplashScreenActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Attributes:
android:background: Sets the background color or image for the splash screen.
android:src: Specifies the image resource for the logo or branding.
android:layout_centerInParent: Positions the ImageView in the center of the RelativeLayout.
This setup creates a simple splash screen that displays for a specific duration (
SPLASH_TIME_OUT) before automatically navigating to the main activity of your app.Remember to replace
@color/splash_background, @drawable/app_logo, and MainActivity::class.java with your actual color, logo, and main activity class, respectively.