Wednesday 6 November 2013

Splash Screen Example

Here i am going to explain Splash Screen in Android, in this example we switch from one activity to another activity automatically after few second. You can use this for starting page of you application.

Example:




After 5 sec it will automatically switch to another activity


Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/whitecolor"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/wait" />

</LinearLayout>

 MainActivity.java

package in.androiddevelopmentanddiscussion.splashscreenexample;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Thread timer = new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{

Intent openMain = new Intent();

openMain.setClass(getApplicationContext(),WelcomeActivity.class);
startActivity(openMain);

}
}
};
timer.start();


}

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


}


activity_welcome.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"
    android:background="@color/whitecolor"
    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=".WelcomeActivity" >

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


</RelativeLayout>

WelcomeActivity.java



package in.androiddevelopmentanddiscussion.splashscreenexample;

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

public class WelcomeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}

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

}


Download full project from here 



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment