Splash Screen in Android Tutorial with Example

Hey Technoz! Hope you are doing great. We have seen many apps in which displays a splash screen at the starting of app for few seconds. So, in this tutorial, we are going to learn how to implement the splash screen in android app. Actually, it is a very easy and quick task. So lets start.

Build the splash screen in android app

Create a new android project or you can continue with your existing one. At first, we need to design our splash screen layout. We will need both java and xml file for this task. So instead of creating only xml file, create a new activity SplashScreenActivity and go to its layout file activity_splash_screen.xml. Write the following code in it.

<?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/colorPrimary">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:layout_width="200sp"
            android:layout_height="200sp"
            android:layout_gravity="center"
            android:src="@drawable/logo" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Technopoints"
            android:textSize="50sp"
            android:gravity="center_horizontal"
            android:layout_marginTop="10sp"
            android:textColor="#ffff"
            android:textStyle="bold"/>
        <ProgressBar
            android:layout_marginTop="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Copyright 2019 Technopoints"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20sp"
        android:textColor="#ffff"
        android:textSize="15sp"/>
</RelativeLayout>

We have put a simple view above to get splash screen in android app. The parent is Relativelayout in which we put the ImageView for app icon, TextView for app name and ProgressBar to show there is an ongoing progress on opening the app. we are covering these three elements in LinearLayout. A bottom line showing a copyright text, is put with alignParentBottom attribute as true.

Prepare the backend

Now, we will do a little work in java file. So, come to SplashScreenActivity.java and write the following code in it.

package net.softglobe.tutorials;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SplashScreenActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        },3000);
    }
}

In above code, we will let this activity (splash screen) run for 3 seconds and then will close and finish this activity and switches to the MainActivity. We have used Handler class for this task. the method postDelayed() performs the task which is intended to do after the completion of delay. We write our Intent code in that method which will switch the user to another activity after finishing timer. The second argument passed in postDelayed()  method is the time (in milliseconds) which is currently 3000. Thus the splash screen in android will run, in this case, for 3 seconds. You can change the time period according to your requirements.

Now we will move to our AndroidManifest.xml file. Make the SplashScreenActivity as a Launcher Activity and declare MainActivity as an ordinary activity. We don’t need any special permissions for this task, so no need to include any of them, in Manifest. You can refer the below code.

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

    <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=".MainActivity"></activity>
        <activity android:name=".SplashScreenActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

That’s it. We have now set up everything to include a splash screen in android app. Try to run the app now. You should be able to see the splash screen running for 3 seconds as in below screenshot.

Splash Screen in Android Tutorial with Example

In above screen, a splash screen is running with a top action bar. If you are okay with it, then you are done. But if you want to remove the action bar, you have to follow some steps below.

Removing an Action bar

If you are using an AppCompat theme, then you need to create a custom theme for your SplashScreenActivity. In order to create a custom theme, go to res->values->styles.xml and paste the following code snippet below your existing code.

<style name="myTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Now, you need to assign this theme to your activity in AndroidManifest.xml. Thus, open the manifest file and add an attribute android:theme="@style/myTheme". So, the activity code will look like

<activity android:name=".SplashScreenActivity"
    android:theme="@style/myTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

You are done. Now try running your application with the new updated code and you will see a splash screen in android app running in a full screen mode as shown below.

Splash Screen in Android Tutorial with Example

Finally we have completed with our tutorial to add splash screen in android app. Hope you like it. If you have any queries, feel free to ask in comment section below.

Download Source Code

If you want to jump directly to the source code, you can download it for free using the below link.

Please subscribe to our newsletter to catch such more knowledgeable and understanding tutorials. 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.