In this post I’ll explain how to create a really bare-bones splash screen for an Android screen. I’ll be assuming that you don’t know very much at all about Android development, so we can simulate my experience with this. I’ll also be assuming that you’ve set your environment up for Android development per Google’s basic documentation.
Creating the Basic Android Project
Go to File –> New –> Android Project, and enter your defaults as below.
Click Finish. In the Package Explorer, expand the SplashScreenSample –> src node until you can see all the children.
Double click the onCreate(Bundle) : void node to view the source code in Eclipse.
This is the default activity that launches when your application starts up. To verify, run the project by clicking on the Run –> Run menu option, then selecting the Android Application option.
After some time spinning up, you’ll see your app running in the emulator. This is essentially the Hello World app, but I put the steps in here just in case you’re new to Android development and Eclipse in general. I was, and this kind of walkthrough would have helped a lot.
Adding a Screen
The first thing we need to do is add another screen to the project, which will be what we display after the splash screen has run its course. The first step in adding a screen is to add an activity to the Android Manifest. First, double-click the AndroidManifest.xml file.
Click the Application tab at the bottom.
We need to add another Activity to our application, so click the Add button in the bottom half of the screen, in the Application Nodes section. Select Activity and click Ok.
You’ll see a whole slew of options for your new Activity. Let’s just name it mainmenu with a Launch mode of standard. Notice that when we enter a name for our activity, Eclipse complains to us that there is no matching class.
Let’s rectify this by adding a class called mainmenu to our application. Right click on the src node in the Package Explorer and select New –> Class. Call the class mainmenu and click Finish. Then add code to your mainmenu class to make it appear as below.
package com.testing.splashscreensample; import android.app.Activity; import android.os.Bundle; public class mainmenu extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
Now we have the screen that we will show after our splash screen finishes displaying.Finishing Up The Splash Screen
Our next step is to add the image you want the splash screen to show. Pick whatever image you want to be displayed as the splash screen, and copy it into the three drawable-* directories under the res node in your project. You can use Explorer-like functionality to do this.
Next we need to define the layout for our splash screen. We start by expanding the res node in Package Explorer, right clicking on the res –> layout node and selecting Android XML File. Call the file splash.xml and click on Finish.
Open up the splash.xml file by double-clicking it and ensure that the xml itself looks like the below. You can see the actual xml in the file by clicking the splash.xml tab.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@drawable/thumbsup" android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> </LinearLayout>We’ve basically added an ImageView control that is pointing to the image we just added to our project, via the android:src attribute on the tag element.
The last step is to add code to the SplashScreen class file. Double click the SplashScreen.java node and copy and paste the below code into the file.
package com.testing.splashscreensample; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class SplashScreen extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); Thread splashThread = new Thread() { @Override public void run() { try { int waited = 0; while (waited < 5000) { sleep(100); waited += 100; } } catch (InterruptedException e) { // do nothing } finally { finish(); Intent i = new Intent(); i.setClassName("com.testing.splashscreensample", "com.testing.splashscreensample.mainmenu"); startActivity(i); } } }; splashThread.start(); } }Run the app, and you’ll see your splash screen, followed by your main screen, and that’s that! We’ve obviously glossed over lots of fundamentals here, but I’ll be covering those in more detail in the weeks to come.

Thanks bro, worked like a charm
Dude, this doesn’t work on the phone, works only on the emulator
any ideas? my phone runs 2.3.4 while the sim 2.2
Why is the main screen smaller in pixel size compared to the splash screen ?
use this. May be it will solve your problem. :s
Ahem… doing the splash screen this way is a horrible idea.
The inner loop of the delay process is something known as a “busy loop.” Please avoid doing it that way, as Android provides a better way to sleep for a specified amount of time without locking up the user interface, or wasting limited device resources.
Instead (if you really must create a splash screen) look up TimerTask.
Did it with timer task this way. Im still looking to start the main activity before starting the timer.
int timeSplashScreenShowsInMilliseconds = 1000;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
finish();
Intent mainActivity = new Intent();
String activityPackageName = MapMyFeelingsActivity.class.getPackage().getName();
String activityClassName = MapMyFeelingsActivity.class.getName();
mainActivity.setClassName(activityPackageName, activityClassName);
startActivity(mainActivity);
}
}, timeSplashScreenShowsInMilliseconds);
Actually I gave up the idea of a timer and instead I use an android animation.
Let’s say it’s an animation that fades out from alpha 1 to alpha 0.
I create a new xml file in res/anim
Then in my activity I first bring my spashscreen layout up front
private void showSplashScreen(){
FrameLayout mainMenu = (FrameLayout) findViewById(R.id.main_menu_layout);
LinearLayout splashScreen = (LinearLayout) findViewById(R.id.splash_screen);
mainMenu.bringChildToFront(splashScreen);
}
And then I fade it out like that :
private void removeSplashScreenAfterSomeTime(){
final Animation toDisappearance = AnimationUtils.loadAnimation(this, R.anim.alpha);
toDisappearance.reset();
LinearLayout splashScreen = (LinearLayout) findViewById(R.id.splash_screen);
splashScreen.startAnimation(toDisappearance);
toDisappearance.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onAnimationEnd(Animation animation) {
LinearLayout splashScreen = (LinearLayout) findViewById(R.id.splash_screen);
splashScreen.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
}
});
}
sorry, here is the alpha.xml file :
damn the site makes my xml disappear lol
anyway I got it from this tutorial : http://www.hascode.com/2010/09/playing-around-with-the-android-animation-framework/
note that this has the advantage of loading your activity while the spash screen displays
If you don’t want a fade out effect you could alpha from 1.0 to 1.0 ?
nice work thnks used for me…..
hi sir, present am developing an app for a game concept so can u plz help me because am unable to fix the errors,
plz can u contact me by mail r send me the details to my mail id so that i can contact u.
reply to:safder.it@gmail.com
it is very useful for developing my andorid sample application.
Thanx.This code is working fine as i wanted it to be
in r.java it says invalid VariableDeclaratorId
thanks a lot bro……. if its really resource expensive to put the thread on loop as pointed out by someone please try to give us improvised code…. im working on it now….if i get any solution, ll post it here….
this code worked like charm…!!
Nice Article…
Really it was Helpful…
This is tutorial is not good for Beginners….
Thanks for your Help !!!!
By Prabhu………
why dont we just use handler
handler.postDelayed(new Runnable(){
start xxxxx
} , 3000) ;