Hi everyone, welcome to another useful tutorial for beginners. So this post will show you an Android Splash Screen Example. An splash screen is a screen that shows up when we launch the app. Splash Screen is very common term and it is required for all the application. The idea behind this is, to show user a welcome screen while your application is loading.
In this android splash screen example we are going to learn how do we implement a splash screen in our application. So let’s begin.
Android Splash Screen Example
For building the Splash Screen the first thing needed is the design of the screen. But as this post is only an example we will not dig much into the designing part. Here I am going to use only a logo that I will display in the center of the screen.
Now let’s just create our android project.
- Here I have created a new project using an EmptyActivity.
- Once your project is loaded, first we will design our Splash Screen.
Designing Splash Screen
You can play with the design as much as you want. Here I am just using logo of StackOverflow to create a very quick design. It is having nothing just the logo image on the center.
For designing the Splash Screen we will create a drawable resource file.
- Right click on the drawable folder and create a new drawable resource file. Here I have created splash_screen.xml.
- Here we are going to design our Splash Screen. So inside the file put the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <color android:color="#ffffff"></color> </item> <item android:width="170dp" android:height="180dp" android:drawable="@drawable/app_logo" android:gravity="center_horizontal|center_vertical" /> </layer-list> |
- In the above code app_logo is the image file that I have already pasted inside my drawable folder, you need to put your logo that you want in the splash screen.
- Now we will create a theme that will show this designed screen as the background of our activity.
Creating Splash Theme
- Open the file res->values->styles.xml and modify it as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- Android Splash Screen Example --> <style name="AppTheme.Launcher" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash_screen</item> </style> </resources> |
- So we have a theme that will show up our Splash Screen. As you can see for the windowBackground of the theme we have set the splash_screen.xml.
Displaying Splash Screen
Let me first tell you the idea. The splash screen will show up only when the activity is not loaded, once the activity is loaded the splash screen will disappear. That is why we have set the splash screen as the windowBackground, so it will so the splash screen only when the activity is not loaded.
- To achieve this objective we will change our MainActivity theme with the theme we created. So come inside AndroidManifest.xml and change the theme to App.Launcher, as shown in the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.simplifiedcoding.androidsplashscreen"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme.Launcher"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
- Now when we launch the activity it will show the Splash Screen, and when the activity is loaded we will change the theme and then it will display our Activity.
- So come inside MainActivity.java and change the theme inside the method onCreate().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package net.simplifiedcoding.androidsplashscreen; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { //Setting the AppTheme to display the activity setTheme(R.style.AppTheme); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //then you can do the rest of the activity thing } } |
- Now run the application and you will see your designed splash screen before loading the activity. It may be too quick if your device is very fast.
So that is all for this Android Splash Screen Example friends. I hope you found it helpful. For any kind of question you can leave your comments.
And if you think the post is helpful please share this post with your friends. Thank You 🙂