Tuesday, August 30, 2011

Create a simple splash screen in android

Step 1 : Create a new project in android.(here we named it Demo)

Step 2 : Get the image that you want to show on splash screen of the project.

Step 3 : Add the image in the 3 drawable folders under resources. (as shown below)




















Step 4 : For displaying splash screen create new class SplashActivity under src folder.(as shown below)
for the time being copy the code from DemoActivity class and add it to newly created Splashactivity class.
(similarly create another class HomeActivity i.e. the screen we are displaying after the splash screen)




















Step 5 : Now we need to add a newly created activity for displaying splash screen.to add new activity click on AndroidManifest.xml (on left project navigation pane.).


Step 6 : Click on applications tab on Manifest file.
Now click on the Add button on the at bottom half of the screen.new screen will appear. select the activity option from it (as shown below).




















Step 7 : Name the activity by browse button for splash activity.(repeat the same for HomeActivity).


















Step 8 : Create a splash.xml file under res/layout folder.
 splash.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
      <ImageView android:src="@drawable/splash"
         android:id="@+id/ImageView01"
         android:layout_width="fill_parent" android:layout_height="fill_parent">
      </ImageView>
</LinearLayout>


Step 9 : Now add the code (given below) to the class SplashActivity
SplashActivity.java
package com.myprograms.demo;

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

public class SplashActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread splashThread = new Thread() {
           @Override
           public void run() {
              try {
                 sleep(5000);
              } catch (InterruptedException e) {
                 // do nothing
              } finally {
                 finish();
                 Intent i = new Intent();
                 i.setClassName("com.myprograms.demo","com.myprograms.demo.HomeActivity");
                 startActivity(i);
              }
           }
        };
        splashThread.start();
    }
 }



HomeActivity.java

package com.myprograms.demo;

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

public class HomeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
Step 10 : Now run the project .and you will see the splash screen followed by home screen.

No comments:

Post a Comment