Android

How to add splash screen in android app using android studio ?

splash-screen-in-android-studio1

The splash screen is an important part of every application it helps us to display details about our company name and apps details to our users in an attractive way. Sometimes we can use it to cover apps loading times and background processing delays.

In this tutorial, we are going to implement a splash screen with a GIF Loading image that makes it more attractive and premium.

Step 1

Add new Layout and name it splash_activity

add-layout

 

Step 2

For using GIF animation in your project add ‘pl.droidsonroids.gif:android-gif-drawable:1.2.2’ dependency in your build.gradle (Module:app)

compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.2'

Step 3

Add these codes to your xml splash_activity

<?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="#FFFFFF"
 android:orientation="vertical" >


 <ImageView 
 android:layout_width="120dp"
 android:layout_height="120dp"
 android:src="@drawable/kllmn"
 android:scaleType="centerCrop"
 android:layout_marginBottom="63dp"
 android:id="@+id/imageView7"
 android:layout_above="@+id/gifImageView"
 android:layout_centerHorizontal="true" />


 <pl.droidsonroids.gif.GifImageView
 android:layout_width="115dp"
 android:layout_height="85dp"
 android:src="@drawable/gggggg"
 android:id="@+id/gifImageView"
 android:layout_marginBottom="81dp"
 android:layout_above="@+id/imageView1"
 android:layout_centerHorizontal="true">
 </pl.droidsonroids.gif.GifImageView>

 <ImageView
 android:layout_width="80dp"
 android:layout_height="38dp"
 android:id="@+id/imageView1"
 android:src="@drawable/textloading"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="7dp" />


</RelativeLayout>

Change  android:src=”@drawable/textloading” in ImageView and android:src=”@drawable/spashgif” with your files

Step 3

Now create a new activity and name it SplashActivity

create-activity-in-android-studio

Insert these codes into your SplashActivity

package com.name.appname;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by Sarun on 24-12-2015.
 */
public class SplashActivity extends Activity
{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  // Need full screen without title bars for splash screen 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
        setContentView(R.layout.splash_activity);
        Thread t = new Thread(){

            public void run(){

                try {

                    Thread.sleep(1000);  // change the time according to your needs(its in milliseconds) 
                    Intent i = new Intent(getApplicationContext(),MainHomeActivity.class);     // change the activity you want to load
                    startActivity(i);

                    finish();

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }

}

We are using a thread for setting display time for splash activity on this method you can change the time according to your needs please keep in mind that time interval is in millisecond format.

When the thread sleeps we call our mainactivity  that need to display after splash screen

Step 4

set the SplashActivity on application launch.

For that move to AndroidManifest.xml and add intent filter tag to SplashActivity

Eg:

<activity
    android:name=".SplashActivity"
    android:label="App name"
    android:screenOrientation="portrait" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

        <action android:name="com.name.appname.MESSAGE" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>