Android – Splash Screen

In android application, splash screen gives beautiful UI design. We often saw many application show its product logo before appear on main screen. It’s main screen appear after 3 to 4 seconds while splash screen displaying. So in this tutorial we will learn step by step and create demonstration android application.

Step 1. 

Open your Android Studio and create new empty project and name it SplashScreen. You may keep any name which do you want.

Step 2.

Now open res=> layout=> activity_main.xml file and add following code:

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

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to Myapplication"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:textColor="#08BE08"/>

</RelativeLayout>

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

Step 3.

 Create a new XML file activity_splash.xml file for SplashScreen and paste the following code in it. This layout contains your app logo or other product logo that you want to show on SplashScreen.

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Splash Screen"
    android:textSize="30dp"
    android:textColor="#B00C02"
    android:layout_centerInParent="true"/>

</RelativeLayout>

Step 4.

Now open app=> java=> package=> MainActivity.java and add the below code.

package com.splashscreen.myapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
@Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      }
}
Step 5.

For SplashScreen we will create a separate splash activity. Now create a new class in your java package and name it as SplashActivity.java.

Step 6.

Add this code in SplashActivity.java activity. In this code handler is used to hold the screen for specific time and once the handler is out, our main Activity will be launched. We are going to hold the SplashScreen for three second’s. We will define the seconds in millisecond’s after Post Delayed(){} method.

3 second =3000 milliseconds.

Post Delayed method will delay the time for 3 seconds. After the delay time is complete, then your main activity will be launched.

package com.splashscreen.myapp;

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

public class SplashActivity extends Activity {

Handler handler;

@Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        handler=new Handler();
        handler.postDelayed(new Runnable() {
@Override
        public void run() {
        Intent intent=new Intent(SplashActivity.this,MainActivity.class);
        startActivity(intent);
    finish();
   }
     },3000);
    }
}

Step 7.

Open AndroidManifest.xml file and make your Splashactivity.java class as Launcher activity and mention the MainActivity as another activity.

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

<application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
<activity android:name=".SplashActivity">
  <intent-filter>
         <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>
<activity android:name=".MainActivity"/>
</application>
</manifest>

Step 8.

Now connect your Android Mobile device with Android Studio for checking output of application. Open one of your project’s activity files and click Run icon from the toolbar.

splashscreen
splashscreen