Saturday, June 2, 2012

Creating a splashscreen in android

A splashscreen can be created in android using threads or countdown timer. Here I have done the splash screen using countdown timer. We have to include the image which we want to display in the <ImageView> in the xml file.
The main.xml file is as follows:

<?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:weightSum="1">
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/splash"
android:id="@+id/imageView1"
android:adjustViewBounds="true">
</ImageView>
</LinearLayout>
 
Next we want to have the java code for the splash screen. Here we want to display an image for 3 seconds and then advance to a new page. This is implemented using countdown timer and the code is as follows:


package pjt.eg.splash;

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

public class Splashscreen extends Activity
{
       public void onCreate(Bundle savedInstanceState)
       {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.main);
                      creatingSplashScreen();  
       }
       private void creatingSplashScreen()
       {
              new CountDownTimer(3000, 1000)
              {
                     public void onTick(long millisUntilFinished)
                     {
             
                     }
                     public void onFinish()
                     {
                           movetohome();
                     }
               }.start();
       }
       private void movetohome()
       {
              Intent i = new Intent(this, Homepage.class);
              startActivity(i);
       }
}

No comments:

Post a Comment