Tuesday, November 8, 2011

Splash Screen in Android (Basic)

A splash screen is an image with animation effects that appears while a game or program is loading. It may also be used to describe an introduction of App or page on a website.

So today we will learn how to create a splash screen in Android so that our app will start with Splash screen and then load Home Screen.

in this example i have defined 5 seconds delay to load home screen you may modify as your need.

so lets try this small app..

My Project structure is look like


-------------------------------------------
App Name: SplashScreenBasic
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: ActivitySplashScreen
-------------------------------------------

ActivitySplashScreen.java

package com.rdc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class ActivitySplashScreen extends Activity {

// set splash duration 3 seconds
private final int SPLASH_DISPLAY_LENGTH = 3000;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// hide the notification bar title on screen
requestWindowFeature(Window.FEATURE_NO_TITLE);

// make full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash_screen);

// create a thread for splash
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
Intent i = new Intent(ActivitySplashScreen.this,
ActivityHomeScreen.class);
startActivity(i);
finish();

overridePendingTransition(R.drawable.splash_fade_in,
R.drawable.splash_fade_out);

}
}, SPLASH_DISPLAY_LENGTH);
}

// disable home button during splash screen
@Override
public void onAttachedToWindow() {
this.getWindow().setType(
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}

// disable back button during splash screen
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}

}

ActivityHomeScreen.java
package com.rdc;

import android.app.Activity;
import android.os.Bundle;

public class ActivityHomeScreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
}

}


create below two xml files in res/layout folder

splash_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/splash_image"
android:src="@drawable/splashscreen"></ImageView>
</LinearLayout>

home_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:text="@string/homeTitle"
android:textSize="25sp"></TextView>
</LinearLayout>

create a drawable folder in resource and put these file for fad animation

splash_fade_in.xml
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000" />

splash_fade_out.xml
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="3000" />

and you AndroidManifest.xml file will be like this.
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".ActivitySplashScreen"
android:screenOrientation="nosensor"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
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=".ActivityHomeScreen"
android:label="Home Screen">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

The app will start like this..


You can download the complete source code zip file here : SplashScreenBasic

 cheers!!

 I'd love to hear your thoughts!

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home