Splash Screen Tutorial With Example In Android Studio

Splash Screen Tutorial With Example In Android Studio

Splash Screen Tutorial With Example In Android Studio
Splash Screen Tutorial With Example In Android Studio

Method 1 of implementing Splash Screen:

Create a thread and set time to sleep after that redirect to main app screen.

			
         /****** Create Thread that will sleep for 5 seconds****/  		
          Thread background = new Thread() {
           public void run() {
              try {
		// Thread will sleep for 5 seconds
		sleep(5*1000);
					
		// After 5 seconds redirect to another intent
		 Intent i=new Intent(getBaseContext(),FirstScreen.class);
		  startActivity(i);
					
		//Remove activity
		finish();
               } catch (Exception e) {
				}
                            }
		        };
		// start thread
		background.start();

Method 2 of Implementing Splash Screen:

Set time to handler and call Handler().postDelayed, it will call run method of runnable after set time and redirect to main app screen.

Handlers are basically background threads which allows you to communicate with the UI thread (update the UI).

Handlers are subclassed from the Android Handler class and can be used either by specifying a Runnable to be executed when required by the thread, or by overriding the handleMessage() callback method within the Handler subclass which will be called when messages are sent to the handler by a thread.

There are two main uses for a Handler:

a) To schedule messages and Runnables to be executed at some point in the future

b) To enqueue an action to be performed on a different thread than your own.

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

// Using handler with postDelayed called runnable run method

  @Override

     public void run() {

     Intent i = new Intent(MainSplashScreen.this, FirstScreen.class);

      startActivity(i);

 // close this activity

      finish();

         }

       }, 5*1000); // wait for 5 seconds

Splash Screen Image Size For Different Screen Size:

Android provides support for multiple screen sizes and densities, reflecting the many different screen configurations that a device may have. You can prefer below sizes to support Splash Screen on different size smartphones.

Android divides the range of actual screen sizes and densities into:

A set of four generalized sizes: small, normal, large, and xlarge

A set of six generalized densities:

  • ldpi (low) ~120dpi (240x360px)
  • mdpi (medium) ~160dpi (320x480px )
  • hdpi (high) ~240dpi (480x720px)
  • xhdpi (extra-high) ~320dpi (640x960px)
  • xxhdpi (extra-extra-high) ~480dpi (960x1440px)
  • xxxhdpi (extra-extra-extra-high) ~640dpi (1280x1920px)

Step 1 : Create a new project and name it Splashscreen

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code:

In this step we simply added a code to display layout after Splash screen.

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="abhiandroid.com.splashscreen.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World by AbhiAndroid!"
        android:textSize="20sp"
        android:layout_centerInParent="true"/>

Step 6:  Add the below code in MainActivity.java and Create a new SecondActivity.java for splash screen

Here we will not define the setcontentview() for the Splash screen activity because we directly apply the Splash Theme on it. Now we just have to launch the Activity(Second Activity) and then finish the activity by calling finish() method.

MainActivity.java

package bd124.com.splashscreen2;

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

public class MainActivity extends AppCompatActivity {

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

        startActivity(new Intent(MainActivity.this,SecondActivity.class));

        // close splash activity
        finish();
    }
}

SecondActivity.java 

package bd124.com.splashscreen2;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by AbhiAndroid
 */

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
Step 7: Theme must be assigned to Main Activity in Android Manifest
Like – (android:name=”.MainActivity”android:theme=”@style/SplashTheme”> )
Here is the full code of AndroidManifest:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <activity android:name="abhiandroid.com.splashscreen2.MainActivity"
            android:theme="@style/SplashTheme">

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

                <category android:name="android.intent.category.LAUNCHER" />
            
        
        <activity android:name="abhiandroid.com.splashscreen2.SecondActivity"/>