Android Articles » Splash Screen

Introduction

The Splash Screen is also referred as the Welcome Screen. Splash screens are usually used by substantially large apps so as to avoid a blank screen, while the app is loading. The actual use of a splash screen is to display the logo of the app and at the same time hide the loading time of the app. It is also known as Launch Screen.


Here is a live demo of the expected output.





Let us see how to implement the Splash Screen in our app.


Start a new project.


Create a new Activity class named SplashActivity with corresponding xml file activity_splash.xml. In the activity_splash.xml page, place an ImageView and a TextView.


				  
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/activity_splash"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#ffffff"
       android:paddingBottom="@dimen/activity_vertical_margin"
       android:paddingLeft="@dimen/activity_horizontal_margin"
       android:paddingRight="@dimen/activity_horizontal_margin"
       android:paddingTop="@dimen/activity_vertical_margin">
   
       <TextView
           android:id="@+id/textView"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:layout_centerHorizontal="true"
           android:layout_marginBottom="16dp"
           android:gravity="center"
           android:text="Loading...." />
   
   
       <ImageView
           android:id="@+id/img_logo"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_centerHorizontal="true"
           android:layout_centerVertical="true"
           android:layout_marginTop="73dp"
           android:src="@drawable/logo_devguru" />
   </RelativeLayout>			  
				  


Now, in the SplashActivity java class write this code.


	
				 
   import android.content.Intent;;
   import android.os.Bundle;
   
   import android.app.Activity;
   
   public class SplashActivity extends Activity {
   
       @Override
       protected void onCreate(Bundle savedInstanceState) {
   
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_splash);
           
           //initialize thread //finally block will excecute after 5 seconds
           Thread timerThread = new Thread() {
               public void run() {
                   try {
                       sleep(5000);//splash time
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   } finally {
                       Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                       startActivity(intent);
                   }
               }
           };
           //start thread
           timerThread.start();
       }
   }
				 


Now go to the AndroidManifest.xml and set the SplashActivity class as the first class and MainActivity as the second class.


					
   <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"></activity>
   </application>					
					 


Now go to the activity_main.xml and place a TextView.


 
				   
   <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">
   
       <TextView
           android:id="@+id/textView2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentTop="true"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="90dp"
           android:text="Welcome to DevGuru Tutorials!"
           android:textAlignment="center"
           android:textColor="#a4a4a4"
           android:textSize="60dp" />
   </RelativeLayout>			
					 


The MainActivity java class is:


 
			 
   import android.support.v7.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);
       }
   }
				
	 


There is a possibility for an error to occur if the png file is not supported or if the project directory have long names.



The error will be

'app:mergeDebugResources' Crunching Cruncher…png failed



In case this error occurs, add this code in the android{} section in the build.gradle(Module:app)



 

   android {
       compileSdkVersion 26
       buildToolsVersion "26.0.0"
       defaultConfig {
           applicationId "your_package_name"
           minSdkVersion 18
           targetSdkVersion 26
           versionCode 1
           versionName "1.0"
           testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
           aaptOptions {
               cruncherEnabled = false
           }
       }
   			 


With this the setup is complete. Now try running the app & you should get the output, you see in the live demo.