Method 1: Using the New Android 12 Splash Screen API (Recommended)
For apps targeting Android 12 (API level 31) and above, you can use the new SplashScreen API for a more modern and streamlined implementation.
- Open Your Project in Android Studio: Start by opening your Android project in Android Studio.
- Update Your
build.gradle
Files:
Ensure that your project is targeting at least Android 12 (API level 31) or higher in your build.gradle
file:
android {
compileSdkVersion 33
defaultConfig {
targetSdkVersion 33
}
}
- Enable the SplashScreen API:
In your
themes.xml
(located in res/values/themes.xml
), add the following style to specify the splash screen theme:
<style name="Theme.MyApp" parent="Theme.SplashScreen">
<!-- Optional: Add a background color or image -->
<item name="windowSplashScreenBackground">@color/splash_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash_logo</item>
<item name="windowSplashScreenIconSize">48dp</item>
<item name="postSplashScreenTheme">@style/Theme.MyApp.Main</item>
</style>
windowSplashScreenBackground
: Defines the background color or image for the splash screen.windowSplashScreenAnimatedIcon
: Points to the splash logo image. The logo should be placed in the drawable
folder.windowSplashScreenIconSize
: Adjusts the size of the splash icon.postSplashScreenTheme
: Defines the theme for the main app screen after the splash screen.
- Set the Splash Screen Theme:
In your
AndroidManifest.xml
, set the splash screen theme to your app's theme:
<application
android:theme="@style/Theme.MyApp"
... >
<activity
android:name=".MainActivity"
android:theme="@style/Theme.MyApp"
android:launchMode="singleTask">
...
</activity>
</application
This ensures that when the app launches, it will first display the splash screen.
- Customize Animation (Optional):
If you want a more customized splash screen animation (such as fading out), you can do so in the
MainActivity
by adding a delay and transitioning after the splash screen.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(() -> {
// Transition to your main screen
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}, 2000); // Show splash screen for 2 seconds
}
Method 2: Creating a Custom Splash Screen (For older Android versions)
If your app targets older Android versions (below API 31), you can create a custom splash screen. This method involves creating a separate activity to display the splash screen and then transitioning to your main activity.
Create a New Activity for Splash Screen:
Right-click the java
folder in your project, and select New → Activity → Empty Activity. Name it SplashActivity
.
Design the Splash Screen Layout:
In res/layout/activity_splash.xml
, define your splash screen UI (e.g., logo, background image).
<?xml version="1.0" encoding="utf-8"?>
<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">
<ImageView
android:id="@+id/splash_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_splash_logo"
android:layout_centerInParent="true"/>
</RelativeLayout>
3. Set SplashActivity to Show for a Few Seconds:
In your SplashActivity.java
(or SplashActivity.kt
if using Kotlin), add a delay and then transition to the main activity:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Use a handler to delay the transition to the main activity
new Handler().postDelayed(() -> {
// Start the main activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish(); // Close the SplashActivity so the user can't go back to it
}, 3000); // Delay for 3 seconds
}
}
4. Set SplashActivity as the Launcher Activity:
In your AndroidManifest.xml
, set SplashActivity
as the main launcher activity:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.MyApp">
<activity
android:name=".SplashActivity"
android:theme="@style/Theme.SplashScreen"
android:label="Splash Screen"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
5. Customize the Splash Screen:
You can add animations like fade-in/fade-out effects to make your splash screen visually appealing.
Example of a fade-out effect:
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(findViewById(R.id.splash_logo), "alpha", 1f, 0f);
fadeOut.setDuration(1000); // Duration of fade out animation
fadeOut.start();