Splash Screen in Android

android
android





What is splash screen?

A graphical element or screen appears for few seconds before an application launch.

When we should use Splash screen?

  1. We can use to show the welcome message when application is launching first time.
  2. While loading the data from  remote system or data base , usually it takes time that time we can show the splash screen(Loading … screen)
  3. When we need to perform any operation and that required time to execute , we can show the splash screen.

Steps

  1. Create one Activity1 and XML file
  2. Design the XML file and put the welcome message
  3. Crete Activity2 and XML file.
  4. Start the Activity1
  5. Start a Thread in Activity1 and sleep for 5 seconds
  6. Start Activity2 from the Thread

There is nothing called readymade splash screen in Android . we can achieve that using above steps.

In a single line, start Activity1 wait for 5 sec then start Activity2.

So user will feel that first screen is splash screen.

In this I am not going to show the steps how to make application using android in android studio.

If you don’t know how to make application in android , please read the hello world in android 

Live Demo

I have created an android application with two Activities

Activity1.java

 

package com.splashscreen;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

public class Activity1 extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_activity1);

Thread t = new Thread(){

public void run(){

try{

sleep(5000);

}

catch(Exception e){

e.printStackTrace();

}

finally {

startActivity(new Intent(new Intent(getApplicationContext(), Activity2.class)));

finish();

}

}

};

t.start();

}

}

Activity2.java

package com.splashscreen;

import android.app.Activity;

import android.os.Bundle;

public class Activity2 extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_activity2);

}

}
splash_screen_android
splash_screen_android

 

activity_activity1.xml

<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" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Activity1"

android:background="@color/highlighted_text_material_dark">

 

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceLarge"

android:text="Welcome to JavaAnt..."

android:id="@+id/textView2"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="172dp" />

</RelativeLayout>

 

activity_activity2.xml

<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" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:context="com.splashscreen.Activity2">

 

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceLarge"

android:text="This is Second Screen"

android:id="@+id/textView"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="148dp" />

</RelativeLayout>

 

AndroidManifest.xml

 

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

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

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="@string/title_activity_activity2" >
</activity>
</application>

</manifest>

Hope it will help you.

Profile photo of Nirmal Dhara
About Nirmal Dhara 28 Articles
Java Developer

Be the first to comment

Leave a Reply

Your email address will not be published.


*