Saturday, July 28, 2012

Splash Screen Demo in android | Splash Page Example in Android


Hello friends,

Today I am going to share very simple code for splash page in android. Splash screen is the first screen in any application. We can add progress bar also on splash page. This is a smple demo for splash page navigation from splash page to main activity page. Hope it will help you..

Screen shot:


1-mainifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.splash.screen.demo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SplashScreenDemo" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NextActivity"/>
    </application>
</manifest>

2-SplshScreenDemo.java file
package com.splash.screen.demo;

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

public class SplashScreenDemo extends Activity {
       private Thread splashThread;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
             
              // The thread to wait for splash screen events
              splashThread = new Thread() {
                     @Override
                     public void run() {
                           try {
                            synchronized (this) {
                             // Wait given period of time or exit on touch
                             wait(3500);
                                  }
                           } catch (InterruptedException ex) {
                           }

                           finish();

                           // Run next activity
                           Intent intent = new Intent();
                           intent.setClass(SplashScreenDemo.this,NextActivity.class);
                           startActivity(intent);
                     }
              };

              splashThread.start();
       }

       /**
        * Processes splash screen touch events
        */
       @Override
       public boolean onTouchEvent(MotionEvent evt) {
              if (evt.getAction() == MotionEvent.ACTION_DOWN) {
                     synchronized (splashThread) {
                           splashThread.notifyAll();
                     }
              }
              return true;
       }
}
4- NextActivity.java file
package com.splash.screen.demo;

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

public class NextActivity extends Activity {
         /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_next);
    }

}
3-main.xml file
<?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:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="center"
        android:layout_marginTop="50sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
       android:layout_gravity="center"/>
</LinearLayout>

4-screen_next.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>


3 comments:

  1. Can you help me please how to add progress bar on splash page?
    Thanks in advance..

    ReplyDelete
    Replies
    1. Hi! you can set an progress bar in your xml or in java file till thread is running..

      public void run() {
      try {
      synchronized (this) {
      // set here your progress bar
      wait(3500);
      }
      } catch (InterruptedException ex) {
      }

      Hope it will work you..

      Delete
  2. after enter to nextactivity i want to scroll the wellcome how?can u explain plse

    ReplyDelete