Android

Splash Screen in Android

How to Create a Splash Screen in Android Studio

In this blog Post, I’m going to guide you on how to create a splash screen in Android Studio. A splash screen is a screen that appears when you open an app, typically displaying a logo or a brand name before the main content loads. This is a great way to enhance the user experience and provide a professional touch to your app.

 

Step-by-Step Guide to Create a Splash Screen

 

Step 1: Create a New Project

  • First, start by creating a new Android project in Android Studio. Name the project as Splash and choose an Empty Activity.

Step 2: Add Your Logo to the Project

  • Copy your logo image into the drawable folder.
  • Ensure the image is in either .png or .jpg format to be compatible with Android Studio.

Step 3: Create a Splash Activity

  • Navigate to the java folder under com.example.splash.
  • Right-click on the package, and select New > Activity > Empty Activity.
  • Name the activity SplashActivity.

Step 4: Modify the AndroidManifest.xml

To make the splash screen the launcher activity, you need to adjust the AndroidManifest.xml file. Replace the launcher activity with the new SplashActivity.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.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/Theme.Splash">

<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".MainActivity" />

<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>

In the above code, the SplashActivity is now set as the launcher activity, which will be displayed first when the app starts.

 

Step 5: Design the Splash Screen Layout

Now, let’s design the layout for the splash screen by creating the activity_splash.xml file in the res/layout folder. Here’s the code for the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<ImageView
android:id="@+id/imageView"
android:layout_width="203dp"
android:layout_height="207dp"
android:layout_centerInParent="true"
app:srcCompat="@drawable/logos"
tools:srcCompat="@drawable/logos" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="136dp"
android:fontFamily="@font/artifika"
android:text="Animi World"
android:textSize="36sp"
android:textStyle="bold" />
</RelativeLayout>

 

This layout includes:

  • An ImageView for the logo.
  • A TextView for the brand name.

Step 6: Add Logic to the Splash Screen

Now let’s write the logic for the splash screen in SplashActivity.java. The goal is to display the splash screen for 3.5 seconds before transitioning to the main activity.

package com.example.splash;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getSupportActionBar().hide(); // Hides the ActionBar for the splash screen

// Creating a new thread to handle the delay
Thread thread = new Thread() {
public void run() {
try {
// Delay of 3.5 seconds (3500 milliseconds)
sleep(3500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Start the MainActivity after the splash screen
Intent openMain = new Intent(SplashActivity.this, MainActivity.class);
startActivity(openMain);
finish(); // Close the splash screen so that the user cannot return to it
}
}
};
thread.start();
}
}

 

In this code:

  • A thread is used to pause the app for 3.5 seconds.
  • After the delay, the app transitions to MainActivity.

Step 7: Run the App

Now, build and run your app. You should see the splash screen with your logo and brand name for 3.5 seconds before it navigates to the main activity.