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..

Prerequisites:

  • Eclipse + ADT plugin
  • Android SDK Tools
  • Android Platform-tools
  • A version of the Android platform
  • A version of the Android system image for the emulator

Step 1 :

Open eclipse and goto  File –> New –> Android Application Project  And create new project with custom name

Step 2 :

Then Right Click on the project name on Package Explorer (Right side panel) and select New and select Class (picture 1.1) here select your Destination Package and in name field type Splashscreen  and press Finish.

Step 3 :

Then copy the following code and past it on Splashscreen.java

Code:

Splashscreen.java

package <your package>

import <your package>.R;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
//import android.util.Log;
import android.util.Log;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Splashscreen extends Activity {
ProgressBar bar;
TextView txt;
int total = 0;
boolean isRunning = false;
// handler for the background updating
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
total = total + 5;
String perc = String.valueOf(total).toString();
txt.setText("Loading..."+perc + " %");
TextView nameverval=(TextView ) findViewById(R.id.namever);
Typeface light = Typeface.create("sans-serif-light", Typeface.NORMAL);
nameverval.setTypeface(light);
nameverval.setText(getString(R.string.app_name) + " V " +getVersion());

if(perc.equalsIgnoreCase("100"))
{
Intent intent = new Intent(Splashscreen.this,MarkersActivity.class);
startActivity(intent);
Splashscreen.this.finish();
}
bar.incrementProgressBy(5);
}

};


private String getVersion() {
String version = "";
try {
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
version = pInfo.versionName;
} catch (NameNotFoundException e1) {
Log.e(this.getClass().getSimpleName(), "Name not found", e1);
}
return version;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
requestWindowFeature(Window.FEATURE_NO_TITLE);
bar = (ProgressBar) findViewById(R.id.ProgressBar1);
txt = (TextView) findViewById(R.id.txt);
}

public void onStart() {
super.onStart();
// reset the bar to the default value of 0
bar.setProgress(0);
// create a thread for updating the progress bar
Thread background = new Thread(new Runnable() {
public void run() {
try {
for (int i = 0; i < 20 && isRunning; i++) {
// wait 1000ms between each update
Thread.sleep(200);

}
}
catch (Throwable t) {              }            }       });
isRunning = true;
// start the background thread
background.start();
}
public void onStop() {        super.onStop();
}}

 

Step 4:

 

Now again Right Click on the project name on Package Explorer (Right side panel) and select New and select Android XML File (picture 1.2) here set Resource type as Layout and in the name field enter splash and select Root element type to RelativeLayot and press Finish

 

 

Step 5:

 

Now past the following code in splash.xml

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash_bg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/namever"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:shadowColor="#7F000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:text="APPS INFOWAY"
        android:textColor="#FFFFFF"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/namever"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="By"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:gravity="center"
        android:text="www.appsinfoway.com\nComplete Mobile App solutions"
        android:textColor="#FFFFFF" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:background="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/txtrere"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:text="Loading..."
        android:textColor="#FFFFFF" />

    <ProgressBar
        android:id="@+id/ProgressBar01"
        style="@style/CustomProgressBar"
        android:layout_width="250dp"
        android:max="100"
        android:layout_height="wrap_content"
        android:layout_above="@+id/txtrere"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dip" />

</RelativeLayout>

Step 6 :

Thats it now Right Click on the project name and select Run As –> Android Application

Output Screen:

appsinfoway_splash_screen

Splash Screen