[font=Arial Black] A simple splash screen[/font]
Alternative which automatically starts another Activity
:src: here.
What you learn: You will learn how to create a simple splashscreen for your application.
Difficulty: 0.5 of 5

Description:
This is a implementation of a simple splash screen. It will show a image on start and removes it after a specific time:
1. Define you splash screen in your layout file:
Using xml Syntax Highlighting
- <?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 id="@+id/splashscreen" android:layout_width="wrap_content"
- android:layout_height="fill_parent" android:src="@drawable/splash"
- android:layout_gravity="center"/>
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="Hello World, splash"/>
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
2. Add a handler to your start activity:
Using java Syntax Highlighting
- private Handler splashHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case STOPSPLASH:
- //remove SplashScreen from view
- splash.setVisibility(View.GONE);
- break;
- }
- super.handleMessage(msg);
- }
- };
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
3. In onCreate create a new message and send it delayed to the handler:
Using java Syntax Highlighting
- Message msg = new Message();
- msg.what = STOPSPLASH;
- splashHandler.sendMessageDelayed(msg, SPLASHTIME);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
Here is the complete code:
Using java Syntax Highlighting
- package com.test.splash;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.widget.ImageView;
- public class splash extends Activity {
- private static final int STOPSPLASH = 0;
- //time in milliseconds
- private static final long SPLASHTIME = 3000;
- private ImageView splash;
- //handler for splash screen
- private Handler splashHandler = new Handler() {
- /* (non-Javadoc)
- * @see android.os.Handler#handleMessage(android.os.Message)
- */
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case STOPSPLASH:
- //remove SplashScreen from view
- splash.setVisibility(View.GONE);
- break;
- }
- super.handleMessage(msg);
- }
- };
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- splash = (ImageView) findViewById(R.id.splashscreen);
- Message msg = new Message();
- msg.what = STOPSPLASH;
- splashHandler.sendMessageDelayed(msg, SPLASHTIME);
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4





