Thursday 26 December 2013

Splash-screen-activity-example-in-android

Simple Splash Screen Activity Example using ProgressBar

In this example i explained how to create splash screen in android.

To create Splash screen we have to use sleep method on Main Thread. i have design it and after completing activity another screen open.

To create this example you have to create two activity 1. SplashActivity ii. HomeActivity

Java Source code for splash screen :

1. SplashActivity.java:

package in.androidshivendra.projectmodel;

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

public class SplashActivity extends Activity {
int i =0 ;
ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
pb  = (ProgressBar)findViewById(R.id.progressBar1);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(i=0;i<=100;i++)
{
pb.setProgress(i);
try {
Thread.sleep(100);
if(i==100)
{
Intent i = new Intent();
i.setClass (SplashActivity.this,HomeActivity.class);
startActivity(i);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t.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, menu);
return true;
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
finish();
super.onPause();
}

}

Xml layout code:-


    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SplashActivity" >

   
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical" >

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

       
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

   


No comments:

Today's Pageviews