Android Splash Screen
If you are an Android user then you might have seen in some apps, specially in games, when the app starts, it shows a logo or an image for certain time 1 or 2 seconds and then enters into home page of app. The first screen in any app which shows some logo or image for certain seconds, are splash screen. Splash screen is used to show user either some progress or advertisement of company, app or developers, before the app loads completely.
In Android, there is no any inbuilt method to implement splash screen as compared to iOS and Windows Phone. In this tutorial, we are going to implement splash screen in our app.
Many apps have many reasons to use splash screen. Some use for showing company logo, some use for downloading data and storing in SQLite database. Some use for fetching and parsing json/xml data and some use for downloading necessary images and preparing app.
Well, in this Android app tutorial, we are using splash screen for app logo display using timer. For this we need two activities, one for splash screen and another for activity after splash screen.
1. Create a new project in Eclipse or Android Studio by following File ⇒ New ⇒ Android Application Project and create an activity. Here I am naming it MainActivity.
2. Create a new class in your main package and name it as SpalshActivity.
3. Now create a layout xml file for SplashActivity inside layout folder situated in res ⇒ layout. and name it as splashactivity.xml. This is the design page of Splash Screen. You can use text, images and animation to this activity. Get the reference from the code below for splashactivity.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/imgLogo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/app_logo" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:textSize="12dp" android:gravity="center_horizontal" android:layout_alignParentBottom="true" android:text="www.pukarm.com.np" /> </RelativeLayout>
4. Go to your AndroidManifest.xml file and open it. Make the SplashActivity as the Launcher activity for this app.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="np.com.pukarm.splashapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- Splash screen --> <activity android:name="np.com.pukarm.splashapp.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> <!-- Main activity --> <activity android:name="np.com.pukarm.splashapp.MainActivity" android:label="@string/app_name" > </activity> </application> </manifest>
5. Now open the SplashActivity.java and use the code below. Here , I have used 4 second timer for splash screen and then it redirects to MainActivity.java.
package np.com.pukarm.splashapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class SplashActivity extends Activity { // Splash Activity time define private static int SPLASH_TIME = 4000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashactivity); new Handler().postDelayed(new Runnable() { @Override public void run() { // After 4 seconds this will run and starts main activity Intent i = new Intent(SplashActivity.this, MainActivity.class); startActivity(i); // close this activity finish(); } }, SPLASH_TIME); } }
Now you can run the application in Android emulator or in device, you’ll see splash screen for 4 seconds and then Main Activity. Here I have used app_logo image. This image should be in drawable folder in your app.