Android Loading Welcome Splash / Spash Screen Example

In android, when we build an application we may need to display a splash screen ( welcome screen ) for users to intimate some thing & do some other process in background ( like fetching data from DB, Parsing XML , etc.. ).

Splash screen is mainly used in Game Applications. Game application may need to show a splash screen that contains App logo, App name & App author of the game to be displayed for few seconds.

Here we are going to see about a simple splash screen example that displays a textview as splash screen & then move to main activity after the splash screen times up.

The output looks similar to

splash.xml

Design a splash screen to display.
Splash.xml file used to show as splash screen. It contains a textview with text as "Splash Screen".

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center" android:background="#6B8AAD">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="18sp"
android:textStyle="bold" android:textColor="#fff" android:text="Splash Screen"></TextView>
</LinearLayout>

[/sourcecode]

SplashScreenActivity.java

Splash screen activity is used to show splash . Here we use thread to show splash for particular time and then disappear automatically.

[sourcecode language="java"]
package com.androidpeople.splash;

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

public class SplashScreenActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/** set time to splash out */
final int welcomeScreenDisplay = 3000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {

int wait = 0;

@Override
public void run() {
try {
super.run();
/**
* use while to get the splash time. Use sleep() to increase
* the wait variable for every 100L.
*/
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
/**
* Called after splash times up. Do some action after splash
* times up. Here we moved to another main activity class
*/
startActivity(new Intent(SplashScreenActivity.this,
MainScreenActivity.class));
finish();
}
}
};
welcomeThread.start();

}
}
[/sourcecode]

MainScreenActivity.java

This main activity is used to show after the splash times up. From here your Apps start up.

[sourcecode language="java"]
public class MainScreenActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

TextView textView = new TextView(this);
textView.setText("Main Activity");
setContentView(textView);
}
}
[/sourcecode]

That's it..

You can download the full source code from here

Comments

Thanks Brayan, The current

Thanks Brayan,

The current activity stops and execute the same activity when you change the phone to landscape mode.The threads get executed twice in this occasion.To stop this we have to manually check and stop the thread when it executes in landscape mode.

Bryan, Look at this link

Bryan,
Look at this link http://www.androidpeople.com/android-how-to-handl...

This describes about how to handle screen orientation in android.

John, First splash.java will

John,
First splash.java will run, after timer ends splash.java will close & then main.java will open.

You can download the source code & check it.
http://www.androidpeople.com/wp-content/uploads/2...

Hong Ann, You are most

Hong Ann,
You are most welcome. Post the code as comment.

Hi Rahul, Replace the

Hi Rahul,
Replace the textview in the splash screen with image view and set the image resource to it.