Saturday, July 24, 2021

Android Splash Screen Tutorial with Example in Android Studio

Android Splash Screen is usually shown at the start of the application for 2 or 3 seconds of time.

Main purpose of Android Splash Screen is to show logo and name of the application to the user.

We will create three types of splash screens in this article.

You can jump to a specific topic using the below table of content.

Step 1. Create a new project

Create a new project in Android Studio with Empty Activity.

Step 2. Code for splash

Copy and paste the following code in activity_main.xml file:

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

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

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <ImageView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:scaleType="fitXY"

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

</RelativeLayout>

Use images of above size for making your splash screen responsive among all the mobile and tablet devices.

Copy and paste following code in MainActivity.java

import android.content.Intent;

import android.os.Handler;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

/** Duration of wait **/

    private final int SPLASH_DISPLAY_LENGTH = 2000; //splash screen will be shown for 2 seconds

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        setContentView(R.layout.activity_main);

        new Handler().postDelayed(new Runnable() {

            @Override

            public void run() {

                Intent mainIntent = new Intent(MainActivity.this, WelcomeActivity.class);

                startActivity(mainIntent);

                finish();

            }

        }, SPLASH_DISPLAY_LENGTH);

    }

}

Second Method

You can implement the splash screen with Thread class also.

Sample source code for using Thread is as the below source code

Thread splashTread = new Thread() {

        @Override

        public void run() {

            try {

                int waited = 0;

                while (_active && (waited < SPLASH_DISPLAY_LENGTH)) {

                    sleep(100);

                    if (_active) {

                        waited += 100;

                    }

                }

            } catch (Exception e) {

            } finally {

                startActivity(new Intent(SplashScreen.this,

                        MainActivity.class));

                finish();

            }

        };

             };

    splashTread.start();

}

In the finally() method, first activity of the android app is called.

To make splash screen with thread, replace the above code with the following code in our existing example

new Handler().postDelayed(new Runnable() {

            @Override

            public void run() {

 

                Intent mainIntent = new Intent(MainActivity.this, WelcomeActivity.class);

                startActivity(mainIntent);

                finish();

            }

        }, SPLASH_DISPLAY_LENGTH);

1 comment:


  1. Thanks for this information. it is helpfull and worthy
    Laptop Repair Center offers quality service for your laptop at a reasonable cost. We offer doorstep support, 24*7 onsite support, repair all brand laptops, have an expert team for onsite support, and much more. for more contact us on 7291903784
    laptop repair center in Delhi

    ReplyDelete