Tutorial

Android Splash Screen

Published on August 3, 2022
author

Anupam Chugh

Android Splash Screen

Android Splash Screen is the first screen visible to the user when the application’s launched. Splash screen is one of the most vital screens in the application since it’s the user’s first experience with the application. Splash screens are used to display some animations (typically of the application logo) and illustrations while some data for the next screens are fetched.

Android Splash Screen

android splash screen Typically, the Activity that has the following intent filter set in the AndroidManifest.xml file is the Splash Activity.

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

Android Splash Screen Example Project Structure

android splash screen example project structure There are few ways to create the initial screen i.e. Splash Screen of the application. Let’s see each of them.

Splash Screen Classical Approach

SplashActivity.java

package com.journaldev.splashscreen;

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

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {


            @Override
            public void run() {
                // This method will be executed once the timer is over
                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, 5000);
    }
}

This is how we normally create the layout of our Splash Screen in our application: activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    tools:context="com.journaldev.splashscreen.SplashActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@id/imageView" />

</android.support.constraint.ConstraintLayout>

Let’s keep the MainActivity.java empty for now. The output produced from the above implementation of SplashScreen is given below. We’ve set the theme of the SplashActivity to Theme.AppCompat.NoActionBar in the AndroidManifest.xml file. android splash screen android studio classical approach Did you see the blank page that came up before the Splash Screen was visible to you? The above approach isn’t the correct approach. It’ll give rise to cold starts. The purpose of a Splash Screen is to quickly display a beautiful screen while the application fetches the relevant content if any (from network calls/database). With the above approach, there’s an additional overhead that the SplashActivity uses to create its layout. It’ll give rise to slow starts to the application which is bad for the user experience (wherein a blank black/white screen appears).

Android Splash Screen Example with Correct Approach

The cold start appears since the application takes time to load the layout file of the Splash Activity. So instead of creating the layout, we’ll use the power of the application theme to create our initial layout. Application theme is instantiated before the layout is created. We’ll set a drawable inside the android:windowBackground attribute that’ll comprise of the Activity’s background and an icon using layer-list as shown below. splash_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/black" />
    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher" />
    </item>
</layer-list>

We’ll set the following style as the theme of the activity. styles.xml

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>

The SplashActivity.java file should look like this:

package com.journaldev.splashscreen;

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

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        new Handler().postDelayed(new Runnable() {


            @Override
            public void run() {
                // This method will be executed once the timer is over
                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, 5000);
    }
}

Note: the theme of the activity is set before anything else. Hence the above approach would give our app a quicker start. android splash screen example android studio Using the theme and removing the layout from the SplashActivity is the correct way to create a splash screen. This brings an end to android splash screen tutorial. You can download the final Android Splash Screen Project from the link below.

Download Android Splash Screen Example Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Anupam Chugh

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 25, 2021

This tutorial is really bad. Lots of times you didn’t mention where things go or what things are. For example you just said create a splash_background.xml but you didnt say inside of drawable, as now people create it inside of layout which doesn’t show layer-list as valid code… Same with SplashActivity, the Handler has multiple import options, none work nowadays. Same with Style, am I supposed to delete the old / default style? Knowledge is stll knowledge so thank you but this has room for improvement.

- Jospeh

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 11, 2019

    what about splash before fragments

    - sharjeel

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      January 2, 2019

      U did miss setContentView(R.layout.splash_screen); ! on the SplashActivity class !

      - Hamma Geek

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        November 5, 2018

        Can’t load bitmaps in xml

        - Deepak

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          May 16, 2018

          Good tutorial, but the theme “SplashTheme” does nothing. It has to be added in the manifest as the theme of the activity. You didn’t add it in the tutorial, only in the example project manifest.

          - Ryan Soemodihardjo

            Try DigitalOcean for free

            Click below to sign up and get $200 of credit to try our products over 60 days!

            Sign up

            Join the Tech Talk
            Success! Thank you! Please check your email for further details.

            Please complete your information!

            Featured on Community

            Get our biweekly newsletter

            Sign up for Infrastructure as a Newsletter.

            Hollie's Hub for Good

            Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

            Become a contributor

            Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

            Welcome to the developer cloud

            DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

            Learn more