Android SplashScreen

Android Splash screen is a UI that is displayed in any android application just before the main UI is displayed. There are different purposes for which we use this screen. In general, we use it for the following purposes.

  • To show the logo of the app: We show the brand / logo etc. of the application using the splash screen. For this purpose, we use timer. 
  • We need to use the application to obtain / fetch basic data from the server: You can use the splash screen to send data to the server and/or download resources (image, files, videos, etc) from the server to be used in the application. 

For example: Download and store data in the database, download required images, parse Json/xml file, send device information and register the device to the server.

  • To show something even before your application is started, i.e. the main activity layout is inflated by the system: As recommended by Google, this should be the actual purpose of the splash screen. It actually takes some time to inflate the main activity layout, especially in Cold Launch, whenever we start android application.

 

You will see white(blank) screen in this duration. Finally, display the splash screen until the main layout of your application is inflated. We can use the logo of our application as a splash screen.

Now-a-days, for any reason (mentioned above) people use android splash screen. So, we're going to use android splash screen.

Hey, Developers ! In this tutorial we will see how to use Android SplashScreen in any android application.

Unfortunately, compared to IOS, there is no built-in mechanism for implementing android splash screen in android apps. So, using activity, we will implement splash screen.

Let's start the tutorial with the creation of new project and the implementation of basic materials. After that, we will start implementing splash screen.

At first, we will create android application. Then, we will see how to build SplashScreen in the application.

Follow steps below to create new project. Please ignore the steps if you have already created a new application.

S. No. Steps
1 Open Android Studio.
2 Click on Start a new Android Studio Project then choose your project and select Empty Activity.
3 Write application name as SplashScreen after this write a package name you can write your own package name.
4 Select language Java. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button.
5 If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial  to Create a New Project to know steps in detail.

Create an activity_splash.xml file in res=>layout folder. And include the following code.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">

<ImageView
android:id="@+id/logo"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@drawable/ukacademe"
android:layout_centerInParent="true"/>
 
</RelativeLayout>

Create an SplashScreenActivity.java file in com.ukacademe.splashscreen. And include the following code.


package com.ukacademe.splashscreen;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

public class SplashScreenActivity extends AppCompatActivity {

    // Splash screen timer
    private static int TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
                startActivity(i);

                // Close this activity
                finish();
            }
        }, TIME_OUT);
    }
}

Now in styles.xml file we added windowActionBar ="false" & windowNoTitle = "true" to hide the ActionBar and and windowTitle to show the splashscreen in full display.

Note :  Its only for splash screen we don't want ActionBar and Title in SplashScreen but we need this both in our main activity or in other activity for that we need to add one more theme for our other activities. So include the following code into your styles.xml file.


<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>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style> 

<style name="AppThemes" parent=&quot 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>

</resources> 

Now, the last step is in AndroidManifest.xml file here we can define our SplashScreenActivity.java file and MainActivity. So include the following code into your AndroidManifest.xml file.

 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.splashscreen">

<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">

<activity android:name=".SplashScreenActivity">
<intent-filter> 
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:theme="@style/AppThemes"></activity>
</application>

</manifest>

Congratulations! your SplashScreen App is completed just run it on your device or in emulator.