Android Splash Screen Tutorial
Android splash screen is used so that apps can show their brand icons before showing the content of app. This may also used to do some background work in your application like loading resources from network while the splash screen is being shown. This will look smooth for user.
Unfortunately in android we don’t have any inbuilt mechanism to show splash screen compared to iOS. In this tutorial we are going to learn how to implement splash screen in your android application.
Our new Tutorial on how to add video in splash screen
When to use splash screen :
- To show your brand logo user.
- To load data from network or other source while showing splash screen.
We will be covering first scenario in this tutorial. One is showing splash screen using a timer.
Logic :
There is nothing magical in this case. We will create two activities , say MainActivity and SplashActivity. Now we will make SplashActivity as "android.intent.category.LAUNCHER"
so that its icon is visible on launcher. When user will click application icon on launcher then the SplashActivity will be called and it will show some UI and after that it will launch the MainActivity .
So let’s get started by creating a new project
Android Splash Screen Using Timer
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details.
2. For Splash Screen we are creating a separate activity. Create a new class in your package and name it as SplashActivity.java
3. Now open AndroidManifest.xml update to following :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.splash" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="21" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <!-- SplashActivity --> <activity android:name="com.example.splash.SplashActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- MainActivity --> <activity android:name="com.example.splash.MainActivity" android:label="@string/app_name" > </activity> </application> </manifest> |
4. Create file activity_splash.xml in res => layout and copy following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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" android:background="#37474f" > <ImageView android:id="@+id/imgLogo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/logo" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="100dp" android:gravity="center_horizontal" android:text="www.androidSRC.net" android:textColor="#ffa000" android:textSize="25sp" /> </RelativeLayout> |
5. Now add following code to SplashActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.example.splash; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class SplashActivity extends Activity { // Splash screen timer private static int SPLASH_TIME_OUT = 3000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); new Handler().postDelayed(new Runnable() { /* * Showing splash screen with a timer. This will be useful when you * want to show case your app logo / company */ @Override public void run() { // This method will be executed once the timer is over // Start your app main activity Intent i = new Intent(SplashActivity.this, MainActivity.class); startActivity(i); // close this activity finish(); } }, SPLASH_TIME_OUT); } } |
6. Now build and run the project. You will see splash screen for 3 second and then the main activity.