Saturday, April 27, 2024
HomeAndroidAndroid Splash Screen Tutorial Using New Android Studio

Android Splash Screen Tutorial Using New Android Studio

-

Android Splash Screen Example:

Many popular Android Apps such as Skype, Facebook, Adobe Reader, 500px, Dropbox etc.,  uses splash screen to display their logo. Most Android Apps uses Android Splash Screen before launching application Activity. Android splash screen is used to display a logo or brand for an app. In this article we are going to discuss about implementing an Android Splash Screen in a simple manner.

First we will create a new default project using these simple steps:

  1. Click on File > New Project.
  2. Next, define Application Name and Minimum SDK and hit Next
  3. Select Blank Activity and Hit Next.
  4. Hit  Finish.

This creates a simple Hello world Project for which we will implement android Splash screen. We already discussed in detail about Creating New Android Project in our previous article.

Related Articles:

How to Test Android App on Real Devices like Android Mobile or Tablet using Android Studio

How to Test Android App on Emulator and setting up Android Virtual Device in New Android Studio

Creating Android Splash Screen:

  1. To display a splash screen, we need a layout first of all. So let’s create a Layout by Right Clicking on Layout (located in App > Res > Layout) and selecting New > Layout Resource File. Give it any name. I gave it splash.xml. Now select Text tab located at bottom as shown in image below.         android splash screen example
  2. Now Open File Explorer in your Operating System and paste the image you want to use as splash screen in Drawable Folder. (Generally located  at C:\Users\UserName\AndroidStudioProjects\AndroidSplashScreenExample\app\src\main\res\drawable) . Advisable Image size is 800 x 1280 pixels and format is jpeg or png. Make sure that you named the image using small letters and underscores without using Special Symbols. I named it as splash_image.[widget id=”text-3″]
  3. Now type the following code in Text Tab of splash.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_image"
android:orientation="vertical"/>

view raw

splash.xml

hosted with ❤ by GitHub

Background is set to splash_image we just copied in Drawable folder. Here layout’s width and height are set to match_parent so that the background image fills the entire Layout.

[adinserter block=”8″]

4. Splash screen is a separate activity which will be displayed first ahead of all other Activity. We create splash screen activity by creating a class. We create a new class by Selecting App > Java > Your Package Name and right Clicking on your package and selecting New > Java Class. Give it any name. Here, I gave it as SplashScreen. Now Type the Following Code in SplashScreen activity:


package com.coderefer.androidsplashscreenexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
/**
* Created by vamsikrishna on 12-Feb-15.
*/
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timerThread = new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
}
}
};
timerThread.start();
}
@Override
protected void onPause() { // TODO Auto-generated method stub
super.onPause();
finish();
}
}

We use methods of Thread class such as –

sleep(long time) – to make the thread sleep for some time (in milliseconds).

start() – to start the thread.

[widget id=”text-3″]

sleep takes the time in milliseconds as its parameter(3000 => 3 seconds). Here we used this delay time as the time to display the splash screen activity. After this delay time, MainActivity is started by the code written in finally{} block.

The splash screen activity must not be shown when the user presses the back button. In order to do this, we should destroy the splash screen activity after it is shown for few seconds. This is done by the use of onPause() method. The onPause() method is a method of Activity class which comes into play when the user leaves the activity.

Next, We need to define which activity to open after displaying Splash screen. This is done using the Intent(Context, Class) constructor of the Intent class.

5. Now to make your splash screen to work, you need to refer the SplashScreen Activity in Android Manifest. So, open the Manifest file by clicking on App > Manifests > AndroidManifest.xml. Now we add reference to new Activity we just created and Change the Launcher Activity (Activity which Launches first) to splash.xml by changing the code shown below:

[adinserter block=”8″]


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.coderefer.androidsplashscreenexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".SplashScreen"
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=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>

6. Now Hit Run Button (or Shift+ F10) and select Emulator or Real Device to run the program. Now you can see Android Splash Screen displaying before your activity.

You can refer to our previous articles on How to run android program on real Device and How to setup and test android project on Emulator for any reference.

[adinserter block=”8″]

51 COMMENTS

    • There might be some typo in your Splash screen’s intent parameter (or) may be in your Main activity’s intent-filter’s action:name which is declared in your manifest. Intent takes action as its parameter. Here it is “com.coderefer.androidsplashscreenexample.MAINACTIVITY”. Make sure you match them exactly.

  1. To get rid of the white screen that precedes the splash screen you might add:
    android:theme=”@android:style/Theme.Translucent.NoTitleBar”
    as an attribute within the activity tag for the splash screen activity.

    Not sure about compatibility with different API levels though.

    And of course: Thank you for the instructions!

  2. finally{
    Intent i = new Intent(SplashScreen.this, MainActivity.class);
    startActivity(i);
    }
    this is better, and more compatible

  3. If my package name is com.example.game and main activity class name is AppActivity what will happen to this line in the manifest file?

  4. If I am extending Activity what should the Activity class look like? My code is not compiling, because I have not created a savedInstanceState variable and InterruptException class. Can someone help me resolve where / how I should do those

  5. I got a confusion here with this line, in my mainfest.xml the code is as follows:

    so what will be the line of code in SplashScreen.java:

    finally{
    Intent openMainActivity= new Intent(“com.coderefer.androidsplashscreenexample.MAINACTIVITY”);
    startActivity(openMainActivity);
    }

    • Hi gilson, check the code from the explanation. You can use
      Intent i = new Intent(SplashScreen.this, MainActivity.class);
      Also you have mentioned the name property as android.intent.action.MAIN. This makes the main activity to launch first. Use a specific unique value such as

  6. Hi Vamsi, thanks for your great tutorial. I am new to android app development, but with your explicit instructions to create a splash screen, I could easily create one and integrate into my app.

    However, I am facing a slight problem with the image. The splash screen shows my image with a vertical stretch. Is there any way I can rectify it ?

  7. Hi.

    The app works great in my phone, but in Genymotion Emulator using Android Studio 1.5 (after importing your project) I get not splash screen with your logo, just a grey splash screen and fynally the main screen.

    Any idea about this behavior?
    Thanks for you work!!

  8. Thanks again, Vamsi: I am merging it with another tutorial by you.

    Let us see what can be done!

  9. I have 6 different splash screen for 6 different screen sizes(mdpi, hdpi, xhdpi etc) How can I display the respective splash screen?

    • change every instance of “com.coderefer.androidsplashscreenexample” to “com.yourcompany.yourappname” and that fixes it.

  10. Hi, on the spash.xml file im trying to add a imageView it acts like its there but its not showing up when i run it on emulator other than that it works perfectly

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

3 Sum | Leetcode #15 | 100 Days of Code Day 10

Today we are going to solve 3 Sum - our first Medium Level Array problem from Coderefer DSA Sheet. If you are new to this...

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called - Best Time...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Two Sum – Day 6 | 100 Days of Code

Welcome to Day 6 of 100 Days of Code where we solve the most frequently asked Easy level Array problem - Two Sum. Let us...

Follow us

1,358FansLike
10FollowersFollow
400SubscribersSubscribe

Most Popular