This Android tutorial demonstrates how to build an Android Application that displays a Splash Screen. In this example the Splash Activity displays at the start of the application and remains there for around three seconds then it automatically goes off the screen and a new Activity or main application starts.
The following example also shows how to use "Message" and "Handler" classes to handle Splash Activity.
As a first step create the Android Project. Select File>New>Android Project in the Eclipse menu.
Now create a class for splash screen that extends the Activity class.
public class SplashActivity extends Activity
Create another class for main activity that will display after the splash activity go off the screen.
public class MainActivity extends Activity {}
Create xml layout files.
Create activity_splash.xml in the res/layout folder. This is used as resource layout file for SplashActivity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textSize ="18sp"
android:text="Splash Activity"
/>
</LinearLayout>
Create another resource layout file “activity_main.xml” for MainActivity class.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize ="18sp" android:text="Main Activity" /> </LinearLayout>
Open AndroidManifest.xml file and define the tag and attributes for both activities.
<activity android:name=".SplashActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="@string/app_name">
</activity>
Open SplashActivity.java file. Create an inner class SplashHandler that extends Handler. Then define a method handleMessage() in this class.
private class SplashHandler extends Handler {
public void handleMessage(Message msg) { }
}
In the onCreate() method of SplashActivity class create an object of type SplashHandler.
SplashHandler mHandler = new SplashHandler();
Next create a Message object.
Message msg = new Message();
Assign a unique code to this message. Later, this code will be used to identify the message in Handler class.
msg.what = 0;
Send the message with a delay by calling sendMessageDelayed(). It will be processed after a delay of 3 seconds (3000 = 3 seconds).
mHandler.sendMessageDelayed(msg, 3000);
Following code shows the implementation of handleMessage() method of Handler class. The method begins by calling the super. In the next step there is a switch statement to handle the messages. Upon receiving the message of interest it creates a new Intent to start the MainActivity. Call to finish() also finishes the current activity(in this case SplashActivity).
// Handler class implementation to handle the message
private class SplashHandler extends Handler {
//This method is used to handle received messages
public void handleMessage(Message msg)
{
// switch to identify the message by its code
switch (msg.what)
{
default:
case 0:
super.handleMessage(msg);
//Create an intent to start the new activity.
// Our intention is to start MainActivity
Intent intent = new Intent();
intent.setClass(SplashActivity.this,MainActivity.class);
startActivity(intent);
// finish the current activity
SplashActivity.this.finish();
}
}
}
Final code for SplashActivity.java
public class SplashActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create an object of type SplashHandler
SplashHandler mHandler = new SplashHandler();
// set the layout for this activity
setContentView(R.layout.activity_splash);
// Create a Message object
Message msg = new Message();
//Assign a unique code to the message.
//Later, this code will be used to identify the message in Handler class.
msg.what = 0;
// Send the message with a delay of 3 seconds(3000 = 3 sec).
mHandler.sendMessageDelayed(msg, 3000);
}
// Handler class implementation to handle the message
private class SplashHandler extends Handler {
//This method is used to handle received messages
public void handleMessage(Message msg)
{
// switch to identify the message by its code
switch (msg.what)
{
default:
case 0:
super.handleMessage(msg);
//Create an intent to start the new activity.
// Our intention is to start MainActivity
Intent intent = new Intent();
intent.setClass(SplashActivity.this,MainActivity.class);
startActivity(intent);
// finish the current activity
SplashActivity.this.finish();
}
}
}
}
Final code for MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Comments
AP (not verified)
Sun, 08/07/2011 - 06:43
Permalink
First one that worked...
WORKS!!!! Nice example. I just spent hours playing around with others that crashed. Great example m8. By far the best Android Splash Screen Example on google!!!
ma (not verified)
Mon, 09/19/2011 - 20:44
Permalink
Great answer and worked very
Great answer and worked very well. Thanks a lot for the post!!!
MIke Albanese (not verified)
Sun, 09/25/2011 - 03:07
Permalink
works
Saved me some time, taught me some stuff.
Very clear and easy to follow.
Shoebob (not verified)
Wed, 11/02/2011 - 22:26
Permalink
Excellent
I have spent a few days trying and testing several methods of "splash" screens. This one worked right from the start. Thanks.
Geraldine (not verified)
Sat, 11/12/2011 - 06:42
Permalink
jePVUCShOkV
Good points all aurnod. Truly appreciated.
Dany (not verified)
Sat, 11/12/2011 - 15:07
Permalink
PYdVCFdkuc
Norlmaly I'm against killing but this article slaughtered my ignorance.
Capatin (not verified)
Sat, 11/12/2011 - 16:21
Permalink
McQREwcnKnOAFlzSuu
There's ntohing like the relief of finding what you're looking for.
Betsy (not verified)
Sat, 11/12/2011 - 18:25
Permalink
RAAQWroiuWBJ
If time is money you've made me a walteiher woman.
Sherlyn (not verified)
Sun, 11/13/2011 - 06:09
Permalink
FhqmaHVJiAxSJvXMhJ
I thank you hmubly for sharing your wisdom JJWY
Vishal (not verified)
Thu, 11/17/2011 - 18:31
Permalink
wonderful
very helpful. Thank you.
Sri (not verified)
Tue, 11/22/2011 - 09:34
Permalink
Good tutorial
Very good one. need more tutorials.. Thanks
sahil (not verified)
Wed, 11/23/2011 - 09:54
Permalink
flawless working. thanks a
flawless working. thanks a lot! :)
chokey (not verified)
Wed, 11/30/2011 - 21:36
Permalink
good contribution
It worked smoothly thanks that has solved my problem. I wish to increase the splash stays on the screen
Kenelm (not verified)
Wed, 01/18/2012 - 07:58
Permalink
GGExepgSzCUd
Surripsignly well-written and informative for a free online article.
Bertha (not verified)
Wed, 01/18/2012 - 15:09
Permalink
gIhSrhoZrEH
Always the best content from these prodigious wtriers.
Melly (not verified)
Wed, 01/18/2012 - 15:22
Permalink
PDtEoKFvBi
If time is money you've made me a waelthier woman.
vishnu (not verified)
Wed, 02/08/2012 - 10:49
Permalink
Thanks
Good and simple example . Keep it up
Add new comment