Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art
HomeF.A.Q.
Community
Writing Recipes
Login
Splash Screens in Android: Method 1
Contributed by Rachee Singh 2011-06-20 10:21:22 (updated 2011-06-22 08:54:03)
In Published Edition? Yes
0
Votes
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:

In 2 seconds, this activity leads to the next activity, which is the standard Hello World Android activity :

Code Repository Subdirectory The code example is in the "SplashDialog" subdirectory of our GitHub repo, and can be viewed at https://github.com/androidcook/Android-Cookbook-Examples/tree/master/SplashDialog You can also download the complete collection of examples at https://github.com/androidcook/Android-Cookbook-Examples/.
Comments (1)
Leave a comment
Edit History (7)
buzzkc said 2011-09-25 10:08:53 To keep the back button from displaying the splash screen upon exiting the app, add the following attribute/value to your <activy> tag in the AndroidManifest.xml. android:noHistory="true" Example: <activity android:noHistory="true" android:name=".SplashScreen" android:windowSoftInputMode="adjustResize|adjustPan"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>