Tuesday 10 September 2013

How to create a Splash Screen?????

Splash Screen in Android


Now we'll learn to create Splash Screen.
Basically the Splash screen is showed to advertise or to load some heavy data previous to starting the Application.

The Example is shown below.

1.activity_main.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" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Loading..."
        android:textSize="24sp" />

</RelativeLayout>

2. MainActivity.java

package com.sri.splash;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(), Second.class);
startActivity(i);
finish();
}
}, 3000);
}

}


Note:Create one new class Second.java and xml layout file second.xml.

3. Second.java

package com.sri.splash;

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

public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}


4. second.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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


5.AndroidManifest.xml

<activity
            android:name="com.sri.splash.MainActivity"
            android:label="@string/app_name" 
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.sri.splash.Second"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
</activity>

6. Don't Forget to add the splash.png image in res/drawable

Thank you.

No comments:

Post a Comment