Building an Android Splash Screen
Splash Screens are a common feature of many apps. They’re a nice way to show off the awesomeness that is your logo or maybe just dazzle your users with some eye candy while your app downloads things from the web, crunches numbers, or calculates the 2000th digit of the number pi. Whatever your reasons are for building a Splash Screen you’d probably like it to have a few nice features. This example shows how to build a Splash Screen that removes the title bar, fixes the screen in Portrait layout and prevents the user’s back button from returning them to the Splash Screen.
Step 1.
Let’s start all the way at the beginning, File -> New Android Project. Feel free to use your own Application and Package Names but make sure you tell it to create a SplashActivity. This will be the brains of our Splash Screen.
Step 2.
We need to add another Activity to our application that we can transition to when our Splash Screen is done. Add a new Activity named MainActivity and create a new layout XML file named splash.xml. Then configure the SplashActivity to use the splash.xml file for it’s layout and the MainActivity to use main.xml. If you’d like to make changes to these layout files to spruce things up go ahead. For this example I’ve added an image to the SplashActivity and changed the text in main.xml to read “Hello World, MainActivity!”
Here’s what your package should look like.
MainActivity
SplashActivity
Step 3.
Once your project is set up, open SplashActivity.java and we’ll make a few changes. Add a variable named splashDelay to hold the length of time to keep the Splash Screen up. Then we’ll use a Timer and a TimerTask to help us schedule the transition to MainActivity. Note that before starting the new Activity we call the finish() method on the SplashActivity. This prevents the user from being able to use the back button to return to this Activity.
Step 4.
Finally, we need to make two changes to AndroidManifest.xml. Set the application element’s theme attribute to Theme.NoTitleBar to get rid of the Title Bar and on the SplashActivity’s activity element, set the screenOrientation attribute to portrait to fix the SplashScreen to portrait layout.
And finally our Splash Screen.
Very useful…………thnx for sharing such a gr8 material..
Very good explanation!!
this code is not working actually…
What problems are you having?
where is the splash XML code? you didnt put it here.
Martinez,
You can get all of this code from Github here: https://github.com/krohling/AndroidSplashScreenExample
I tried this code,,bt i displays only my Main.xml
Bing,
You can download this full project from Github here: https://github.com/krohling/AndroidSplashScreenExample
Let me know if you are still having this problem after cloning from Github.
excellent. Could you give us the source code for the animation?
Raymundo,
I just updated this post to include a link to the Github Repo. You can find all of this code here: https://github.com/krohling/AndroidSplashScreenExample
Awesome, worked perfectly for my project. http://www.logicalmobility.com