Thursday, 21 March 2013

Android Splash Screen Example

Here we can See how to create splash screen in Android Application.
Some times the app need to show some company logo and some loading progress before starting Application at this time Splash Screen is main role.

UI Source(splash_layout.xml)


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashActivity" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Please wait Loading....!" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_above="@+id/textview"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="22dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

Android Activity(SplashActivity.java)

This is splash screen activity here we are using theads to wait some time with in this screen.
after finishing sleep time SplashActivity will be  navigate to HomeActivity using Intents.

package com.androidsurya.splashscreenexample;

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

public class SplashActivity extends Activity {
    private static int SLEEP_TIME = 5500;// no of mille seconds sleep

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_layout);
        // The thread to wait for splash screen events
        Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        // Wait given period of time or exit on touch
                        wait(SLEEP_TIME);
                    }
                } catch (InterruptedException ex) {
                }

                finish();

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

        splashThread.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash_layout, menu);
        return true;
    }

}

UI layout(home_layout.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

Android Activity(HomeActivity.java)

package com.androidsurya.splashscreenexample;

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

public class HomeActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
    }

}

Register both Activity's in AndroidManifest file

<activity
            android:name="com.androidsurya.splashscreenexample.SplashActivity"
            android:label="@string/app_name" >
          
        </activity>
        <activity
            android:name="com.androidsurya.splashscreenexample.HomeActivity"
            android:label="@string/app_name" >

Output Screenshot






















No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...