How to Create Android Splash Screen

How to Create Android Splash Screen

In this tutorial we are going to learn how to create android splash screen. This will be a simple android tutorial. In android development, splash screen can be used in different use case scenarios.

The likely usage is on app start, if you want to load some resources the app will use. It has also find wide usage in android games, for resources loading and network process delay.

This app will make use of a Handle class with a post delay of 3 seconds through a Runnable object. After the 3 seconds delay a new Activity class is stared.

I will not support multiple screen in this tutorial, if you want to support multiple screen in your application then you will need to have different sizes of the background image in the drawable folder. You can read more on this topic Google multiple screen support.

Before we start, the first thing I will do is to list the environment and tools I used in this android tutorial but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidScreen

Package: com.inducesmile.androidscreen

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

Open the activity_main.xml, we are going to add an ImageView with width and height attribute of match_parent. This is the image that a user will see when the app starts. You are free to design your own UI interface the way you want. The image I used in this tutorial can be find in the drawable folder. The complete code snippet for the class is as shown.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/splash_image"
        android:background="@drawable/advancemath"
        android:layout_alignParentLeft="true" />
</RelativeLayout>

In the MainActivity class, we are going to create an instance of the Handle class. The handle class method postDelay(Runnable runnable)  is called and an instance of a Runnable class is passed as a parameter and also the delay time in seconds that the screen will wait before transition to a new Activity class. The transition to the WelcomeActivity class is done through the startActivity()  method of the MainActivity class. The code is as shown.

package inducesmile.com.androidscreen;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    private final int SPLASH_DISPLAY_LENGTH = 3000;

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

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(MainActivity.this, WelcomeActivity.class);
                MainActivity.this.startActivity(mainIntent);
                MainActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

The code in the WelcomeActivity class is very simple and it is included in the main project source code.

Save the file and run your project. If everything works for you, the project will appear like this in your device.

How to Create Android Splash Screen

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

Add a Comment