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

Splash Screens in Android: Method 2

Author: Rachee Singh
Published? true -- FormatLanguage: W

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 untill the Menu button the Android device is not pressed and 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 Menu key by checking the Key code and then finishing the 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) {
		// TODO Auto-generated method stub
		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, Splash.class);
			startActivity(intent);
		}
		if(KeyEvent.KEYCODE_BACK == keyCode) {
			finish();
		}
		return false;
	}
}

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.

No records found.