How to make a splash screen (screen visible when app starts)?

pambuk picture pambuk · Feb 8, 2010 · Viewed 29.6k times · Source

I have a simple application, it starts, loads xml feed from the net, you can browse a list of news and then read details for a chosen news item. What I would like to do is have a splash screen, meaning as soon as you click application, it should display an image (app name in my case) and then display news list only after they've loaded.

I read about similar (I think) problems, and usually people say to use FrameLayout, but I can't really sort it out. I'm not sure if this can be done in the first activity that is launched, maybe I should just display this splash image in one activity and only then call activity displaying my news list?

I know that on iPhone you can set splash screen in app settings while developing, would be nice to have this functionality in android's app's manifest...

Answer

jagsaund picture jagsaund · Feb 9, 2010

Android suggests you take advantage of using a splash screen when performing lengthy calculations on start up. Here's an excerpt from the Android Developer Website - Designing for Responsiveness:

"If your application has a time-consuming initial setup phase, consider showing a splash screen or rendering the main view as quickly as possible and filling in the information asynchronously. In either case, you should indicate somehow that progress is being made, lest the user perceive that the application is frozen." -- Android Developer Site

You can create an activity that shows a Progress Dialog while using an AsyncTask to download the xml feed from the net, parse it, store it to a db (if needed) and then start the Activity that displays the News Feeds. Close the splash Activity by calling finish()

Here's a skeleton code:


public class SplashScreen extends Activity{
   @Override
   public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      // set the content view for your splash screen you defined in an xml file
      setContentView(R.layout.splashscreen);

      // perform other stuff you need to do

      // execute your xml news feed loader
      new AsyncLoadXMLFeed().execute();

   }

   private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void>{
      @Override
      protected void onPreExecute(){
            // show your progress dialog

      }

      @Override
      protected Void doInBackground(Void... voids){
            // load your xml feed asynchronously
      }

      @Override
      protected void onPostExecute(Void params){
            // dismiss your dialog
            // launch your News activity
            Intent intent = new Intent(SplashScreen.this, News.class);
            startActivity(intent);

            // close this activity
            finish();
      }

   }
}

hope that helps!