Tuesday, April 10, 2018

Android Splash Screen (Part-1)

Android Splash Screen is used to show the logo of your company or the apps logo. But it can also be used to acquire some background data or information regarding your app. It is also used to show user some kind of progress before the app loads completely. Unfortunately in android we don’t have any inbuilt mechanism to show splash screen compared to iOS. In this tutorial we are going to learn how to implement splash screen in your android application.

How can a splash screen be used while loading your apps data in background processes? Here are some of the ways using which you can use splash screen in background.
1.) Use it to showcase your app logo (can be dismissed using a timer) 
2.) Use it to make a network call and preparing app
3.) Use it to download the data and store it in Sqlite Database
4.) Use it to download required images
5.) Use it to fetch and parse json/xml data
6.) Use it to send device's information like device id and registering the device to your server



In this tutorial I’ll be covering implementation of splash screen in two different scenarios with two different styles. One of the scenario is showing splash screen using a timer and second is showing splash screen when making network http calls which takes some time to fetch required information. This both have one same way to show the splash screen. Another way of displaying splash screen it by using a theme style in android.
In order to implement splash screen we are going to create a separate activity for splash and once it closes we launch our main activity.

1. Android Splash Screen Using Timer

Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. (I use main activity's name as MainActivity.java).  For Splash Screen we will create a separate activity. Create a new class in your package and name it as SplashActivity.javaOpen your your AndroidManifest.xml file and make your splash screen activity as Launcher activity.

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.android.splashscreendemo"

android:versionCode="1"

android:versionName="1.0" >

<application

    android:allowBackup="true"

    android:icon="@drawable/ic_launcher"

    android:label="@string/app_name"

    android:theme="@style/AppTheme" >

    <!-- Splash screen -->

    <activity

        android:name="com.android.splashscreendemo.SplashActivity"

        android:label="@string/app_name"

        android:screenOrientation="portrait"

        android:theme="@android:style/Theme.Black.NoTitleBar" >

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity>



    <!-- Main activity -->

    <activity

        android:name="com.android.splashscreendemo.MainActivity"

        android:label="@string/app_name" >

    </activity>

 </application>

</manifest>

Create a layout file for splash screen under res ⇒ layout folder. I named the layout file as activity_splash.xml. This layout normally contains your app logo or company logo.

activity_splash.xml
<?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="@drawable/gradient_bg" >

<ImageView

    android:id="@+id/logo"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_centerInParent="true"

    android:src="@drawable/app_logo" />

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginBottom="10dp"

    android:textSize="16sp"

    android:textColor="#454545"

    android:gravity="center_horizontal"

    android:layout_alignParentBottom="true"

    android:text="Splash Screen Demo" />

    </RelativeLayout>

Add the following code in 
SplashActivity.java activity. In this following code a handler is used to wait for specific time and once the timer is out we launch main activity.
SplashActivity.java

package com.android.splashscreendemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashActivity extends Activity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        new Handler().postDelayed(new Runnable() {
            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo
             */

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

Run the application, you will see the splash screen for 3 sec and then your main activity will be launched. This was the first scenario for creating the Splash Screen in Android Studio. Now, the next scenario which is creating a Splash Screen with a Network call will be explained in the next post. 

0 Comments:

Post a Comment