tisdag 29 maj 2012

How to create a splash screen in Android

Splash screens are not really an Android thing.  Most Android users would like their app to start as fast as possible and will be annoyed by a splash screen. So avoid it in your own apps, and try to talk customers out of using it.  However there are situations where it might be needed, one of them would be if your app need to load some large amount of data before it can be run. In that case the splash gives can tell the user that the application is loading.

The easiest way to create a splash  would be to create it as a separate activity that dies after a while.
Something like this would do.  Just put your splash image e.g. as a png image in your drawable folder
and start the Splash activity by sending an intent  from your main app code.


import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;

public class Splash extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  ImageView v = new ImageView(this);
  v.setImageResource(R.drawable.splash);
  setContentView(v);
  AsyncTask t = new AsyncTask(){

   @Override
   protected Void doInBackground(Void... params) {
    try {
     //Show the splash one second
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    return null;
   }

   @Override
   protected void onPostExecute(Void result) {
    finish();
   }
   
  };
  t.execute();
  
 }
}


Inga kommentarer:

Skicka en kommentar