Design Splash Screen – Android Application

Design Splash Screen – Android Application

Hello folks welcome back, previously we have seen many android tutorials but i probably this one really going to help you out. As every android application need its wel come screen – Splash Screen (Technical  Term) for its users this article help you out where i have discussed design Splash Screen – Android Application.

Start with an new Android Application Project by giving its proper details and all other related fields. You will get AndroidManifest file, JAVA file and activity layout files ready for you. The extra things we need is an activity that will represent the Splash Screen. So create an another activity (XML and JAVA files) and register it to Application Manifest file. Make sure you have got register your newly created Activity in Manifest file. Also override onCreate() method of an activity inside JAVA file of newly created Activity. Additionally, if you want to add some design to your splash screen you can add the elements in the XML file as you require. I suggest you to add some design element so you can have feel of real Splash Screen. You can also put WelCome messages to your MainActivity that will display wright after your SplashScreen. So add the changes as you require where i have just included one ImageView and give it source of app_icon from resources and a TextView below it.

splash_screen.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/app_icon" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Loading ..."
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30sp"
        android:textStyle="italic" />

</RelativeLayout>

Now, inside that method create a new Thread(). Inside the Thread pass new Runnable() as an argument that will override run() method of Thread class. Inside run method, keep thread sleep for some time duration with the help of Thread.sleep(milliseconds) & giving proper value of milliseconds. (i.e 2000 (2 sec), 3000 (3 sec)) The IDE will ask you to cover Thread.sleep() with try and  catch clause, do it accordingly cause it requires to handle InterruptedException. The number that you will specify in the sleep() method will be the number of seconds the activity waits to process further.

SplashScreenActivity.java

package com.purvik.splashDemo;

import android.app.Activity;
import android.content.Intent;

public class SplashScreen extends Activity {
    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_screen);	//SET THE LAYOUT OF ACTIVITY

		new Thread(new Runnable() {
			
			@Override
			public void run() {
			
					   try {
						   //MAKE THREAD FOR 5 SECOND
						   Thread.sleep(5000);
						
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
						//START MAIN ACTIVITY
						Intent intent = new Intent(SplashScreen.this,HomeScreen.class);
						SplashScreen.this.startActivity(intent);
						
						//TO FINISH SPLACH SCREEN ACTIVITY WHEN IT COMES BACK FROM ACTIVITY STACK
						finish();
					
				
			}
		}).start();	//START THE THREAD
		
	}
}

Wright after the catch clause, declare an Intent object with new Intent(this, MainActiity.class) by passing our MainActivity class. Next, call startActivity(intent) method by passing intent object as parameter. That will start MainActivity of Android Application wright after waiting for number of seconds specified in Thread.sleep() method. After startActivity(), call a single line method finish() of Activity class, that will finish this activity when it comes back to this activity from activity stack. You will get best knowledge why we have called finish() method wright after startActivity() method, when you do comment it & run the application individually. You will see the difference sure.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.purvik.splashDemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

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

</manifest>

So that’s how you can design an effective splash screen for your android application with the help of simple term that is Thread. Check my next updates where i am about to tell you other way to do the same for splash screen designing. 😉

“Bless for You, Peace for the World” – Purvik Rana

Leave a Reply

Your email address will not be published. Required fields are marked *