Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Creating Splash Screens in Android

Rachee Singh

Problem:

Most applications require an introductory opening screen.

Solution:

Such introductory screens are called Splash screens. An activity that is finished within a span of 2-3 seconds or dismissed on the click of a button is a splash screen.

Discussion:

The splash screen displays for 2 seconds and then the main activity of the application appears. For this, we add a java class that displays the splash screen. It uses a thread to wait for 2 seconds and then it uses an intent to start the next activity.


public class SplashScreen extends Activity {
	private long ms=0;
	private long splashTime=2000;
	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, Splash.class);
        		startActivity(intent);
    		}
        	}
    };
    mythread.start(); 
	}

}

The layout of the splash activity, splash.xml is like:


<?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_below="@id/image"
	android:layout_gravity="center_horizontal">
	</ProgressBar>
</LinearLayout>

The splash screen looks like:

Download:

The source code for this project can be downloaded from https://docs.google.com/leaf?id=0B_rESQKgad5LZGY1N2RjYzQtZGQxNC00Njk5LWIyM2ItNDdlN2IwZjg4MmVj&hl=en_US&authkey=COOL9NwM.

Download:

The source code for this project is in the Android Cookbook repository at http://github.com/IanDarwin/Android-Cookbook-Examples, in the subdirectory SplashDialog.