Creating Splash Screens in Android

Splash Screen is most commonly the first startup screen which appears when App is opened. In other words, it is a simple constant screen for a fixed amount of time which is used to display the company logo, company name or advertising content etc.

You can construct a splash screen as an Activity or as a dialog.

Let’s Start

The splash screen was originally invented in the PC era, initially as a cover-up for slow GUI construction when PCs were slow. Vendors have kept them for branding purposes. But in the mobile world, where the longest app start-up time is probably less than a second, people are starting to recognize that splash screens have become somewhat anachronistic.

There are two methods of handling the splash screen in android.

The first version uses an Activity that is dedicated to displaying the splash screen. The splash screen displays for two seconds or until the user presses the Menu key, and then the main Activity of the application appears. First we use a thread to wait for a fixed number of seconds, and then we use an Intent to start the real main Activity. The one downside to this method is that your “main” Activity in your AndroidManifest.xml file is the splash Activity, not your real main Activity.

public class SplashScreen extends Activity {
    private long ms=0;
    private long splashTime=1800;
    private boolean splashActive = true;
    private boolean paused=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread mythread = new Thread() {
            public void run() {
                try {
                    while (splashActive && ms < splashTime) {
                        if(!paused)
                            ms=ms+100;
                        sleep(100);
                    }
                } catch(Exception e) {}
                finally {
                    Intent intent = new Intent(SplashScreen.this, Main.class);
                    startActivity(intent);
                }
            }
        };
        mythread.start();
    }

}

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView android:src="@drawable/background"
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <ProgressBar android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal">
    </ProgressBar>
</LinearLayout>

One additional requirement is to put the attribute android:noHistory="true" on the splash Activity in your AndroidManifest.xml file so that this Activity will not appear in the history stack, meaning if the user uses the Back button from the main app he will go to the expected Home screen, not back into your splash screen.

Creating Splash Screens in Android demo image 1

Two seconds later, this Activity leads to the next Activity, which is the standard “Hello, World” Android Activity, as a proxy for your application’s main Activity.

Creating Splash Screens in Android demo image 2

In the second version, the splash screen displays until the Menu key on the Android device is pressed, then the main Activity of the application appears.  For this, we add a Java class that displays the splash screen. We check for the pressing of the Menu key by checking the KeyCode and then finishing the Activity.

public class SplashScreen extends Activity {
    private long ms=0;
    private long splashTime=1800;
    private boolean splashActive = true;
    private boolean paused=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode, event);
        if (KeyEvent.KEYCODE_MENU == keyCode) {
            Intent intent = new Intent(SplashScreen.this, Main.class);
            startActivity(intent);
        }
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            finish();
        }
        return false;
    }
}

As before, after the button press this Activity leads to the next Activity, which represents the main Activity.

The other major method involves use of a dialog, started from the onCreate() method in your main method. This has a number of advantages, including a simpler Activity stack and the fact that you don’t need an extra Activity that’s only used for the first few seconds. The disadvantage is that it takes a bit more code, as you can see.

public class SplashDialog extends Activity {
    private Dialog splashDialog;

    /** Called when the Activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StateSaver data = (StateSaver) getLastNonConfigurationInstance();
        if (data != null) { // "All this has happened before"
            if (data.showSplashScreen ) { // And we didn't already finish
                showSplashScreen();
            }
            setContentView(R.layout.main);
            // Do any UI rebuilding here using saved state
        } else {
            showSplashScreen();
            setContentView(R.layout.main);
            // Start any heavy-duty loading here, but on its own thread
        }
    }

The basic idea is to display the splash dialog at application startup, but also to redisplay it if you get, for example, an orientation change while the splash screen is running, and to be careful to remove it at the correct time if the user backs out or if the timer expires while the splash screen is running.

Muhammad Mubeen

Muhammad Mubeen

Mubeen is a full-stack web & mobile app developer who is very proficient in MEAN.js, Vue, Python, Ionic 4, Flutter, Firebase, ROR, and PHP. He has created multiple mobile and web applications. He is very passionate about sharing his knowledge.

Leave a Reply

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

Trending