First of all, what is a SplashScreen?
A splash screen is a screen which appears when you open an app on your mobile device. So, we can say it's the first impression for the user. It's usually used to show the logo of the app or an image related with the app.
In my personal opinion, this is the most simple way to implement a static splash screen.
So, let's code it.
First of all, create a new android project with a empty activity called SplashActivity.
Once that it's done we are ready to implement our SplashScreen:
1. Create a custom style in styles.xml under the res/values folder.
2. On the AndroidManifest.xml, add the theme we created before to the SplashActivity.
3. Add to the drawable folder any image you want, we gonna use it in the SplashScreen.
4. Modify the activity_splash.xml.
5. Add a new activity. I will called it MainActivity.
6. Now, implement this code in the SplashActivity. I'll use a Handler to create a small delay and then go to the MainActivity.
And that's all! You have a SplashScreen in your app. Of course, you can use an animation and make it really awesome but I'll show it in another post.
Thanks for reading!
Here is the repo just in case:
Top comments (6)
I prefer doing it without an Activity: create a style for the splash screen and set it as the main activity's style. Then, on the activity's onCreate, set the theme to the default theme and you're done. This immediately displays the splash screen (without that white screen that is sometimes displayed) and eliminates the need for an extra activity just to show the splash screen. However, it has the disadvantage of showing the splash screen again if you navigate to another activity and the main activity gets destroyed (for instance, due to resource constraints).
Regardless, since I've been using a single activity - multiple fragments architecture, the disadvantage doesn't really bother me :)
Looks good because of your architecture but what if you wanna do an animation with the image? or something like that?
You can add references to animations in a theme. You could, for example, create an animation-list with the animation you want and set it as the theme's windowBackground.
can you show me the way in steps plz?
I have an example of this in a repo:
1 - Create the splash screen style like in github.com/rcosteira79/AndroidMult...
2 - Set it as the main activity's theme like in github.com/rcosteira79/AndroidMult...
3 - Reset the theme in the main activity like in github.com/rcosteira79/AndroidMult...
Thanks Ricardo, this works perfectly !