Thursday, April 12, 2012

Android - Splash Screen Example


Android - Splash Screen Example






SPLASH SCREEN

SOURCE CODE [splash.xml] is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/ splash "
android:id="@+id/imageView1"
                                android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
                                android:layout_width="250px"
android:layout_height="400px">
                </ImageView>

</RelativeLayout>

SOURCE CODE [SplashScreenExample.java] is

package com.SplashScreenExample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

public class SplashScreenExample extends Activity
{

                // how long until we go to the next activity
                protected int _splashTime = 5000;

                private Thread splashTread;

                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState)
{

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.splash);

                                final SplashScreenExample sPlashScreen = this;

                                // thread for displaying the SplashScreen
                                splashTread = new Thread()
{
                                                @Override
                                                public void run()
{
                                                                try {
                                                                                synchronized (this) {
// wait 5 sec
                                                                                                wait(_splashTime);
                                                                                }
                                                                } 
catch (InterruptedException e)
{
                //Exception handling
                                                                }
finally
{
                                                                                finish();

                                                                                // start a new activity
                                                                                Intent i = new Intent();
                                                                                i.setClass(sPlashScreen, MainActivity.class);
                                                                                startActivity(i);

                                                                                stop();
                                                                }
                                                }
                                };

                                splashTread.start();
                }

                // Function that will handle the touch
                @Override
                public boolean onTouchEvent(MotionEvent event)
{
                                if (event.getAction() == MotionEvent.ACTION_DOWN)
{
                                                synchronized (splashTread)
{
                                                                splashTread.notifyAll();
                                                }
                                }
                                return true;
                }

}

No comments: