AndroidAndroid UI

Create Splash Screen In Android

What Is A Splash Screen?

Splash screen is the initial screen when user opens the app. It basically contains branding items like logo, slogan and company name etc. It also known as the boot screen. If you want to execute a long task and get the result to show as the content of the next page then Splash screen is the best place to execute that long running task.

See the picture below, is an example of what the DropBox splash screen for Android that appears when the app is opening.

splashscreen

In this tutorial i will show you how to create a simple splash screen that waits for 5 seconds before it automatically navigates to the next page.

Step 1: Create A Layout XML file

Create a layout res/layout/activity_splash.xml

splash layout

Add a TextView with text “Please wait …”. Which will be the indicator for the viewers that the app is still responding.

Step 2: Create SplashActivity Class

import android.content.Intent;
import android.os.Handler;
import android.app.Activity;
import android.os.Bundle;

public class SplashActivity extends Activity {
    private Handler mWaitHandler = new Handler();

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

        mWaitHandler.postDelayed(new Runnable() {

            @Override
            public void run() {

                //The following code will execute after the 5 seconds.

                try {

                    //Go to next page i.e, start the next activity.
                    Intent intent = new Intent(getApplicationContext(), NextActivity.class);
                    startActivity(intent);

                    //Let's Finish Splash Activity since we don't want to show this when user press back button.
                    finish();  
                } catch (Exception ignored) {
                    ignored.printStackTrace();
                }
            }
        }, 5000);  // Give a 5 seconds delay.
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        
        //Remove all the callbacks otherwise navigation will execute even after activity is killed or closed. 
        mWaitHandler.removeCallbacksAndMessages(null);
    }
}

Step 3: Add Activity In Manifest

Finally Set Splash activity as the first page when app opens.

   ...
   ...

 <activity
            android:name=".SplashActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Good luck!

About author

Geethu is a teacher by profession, but she likes to research new things and is also interested in writing. Devdeeds is her blog, where she writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

Your email address will not be published. Required fields are marked *