Friday 22 March 2013

Simple Android Splash Screen


Hi Every one ,In this tutorial I’m going to show you how to easily create an android splash screen in your application.
The main aim of this splash screen implementation is to start and display an activity for a certain amount of time and then start new activity.

Step 1:

SplashScreen.java:


package com.androidpeoplesconnect.app;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;

/* Splash screen activity */
public class SplashScreen extends Activity {

       private static final int SPLASH_DURATION = 2000; // 2 seconds

       boolean mIsBackButtonPressed;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);

        Handler handler = new Handler();

        // run a thread after 2 seconds to start the home screen
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {

                // make sure we close the splash screen so the user won't come back when it presses back key

                finish();
               
                if (!mIsBackButtonPressed) {
                    // start the home screen if the back button wasn't pressed already
                    Intent intent = new Intent(SplashScreen.this, Home.class);
                    SplashScreen.this.startActivity(intent);
               }
               
            }

        }, SPLASH_DURATION); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called

    }

    @Override
   public void onBackPressed() {

        // set the flag to true so the next activity won't start up
        mIsBackButtonPressed = true;
        super.onBackPressed();

    }
}

Step 2:

Create XML File  in res -> layout-> splash.xml



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/splash_screen"/>

Step 3:  Home.java :


import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;




public class Home extends Activity {

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
Toast.makeText(this, "Your in Home", Toast.LENGTH_LONG).show();


}
}


Step 4: Mention the Home.java in  the manifest file

 <activity  android:name=".Home" ></activity>

Step 5: place the splash_screen.png image in drawable folder










No comments:

Post a Comment