Splash Screen in Android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Splash Screen in Android

Postby vivekkalady » Fri Nov 21, 2014 2:09 pm

This is the basic way to create a splash screen

This method uses Handler class and the function postDelayed with 2 seconds delay.

Code: Select all

public class SplashScreen extends Activity {
 
    // Splash screen timer
    private static int SPLASH_TIME_OUT = 2000;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
 
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                finish();
            }
        }, SPLASH_TIME_OUT);
    }
}



you can also use thread to do the same also.

Code: Select all

public class SplashScreen extends Activity {

protected boolean _active = true;
protected int splashTime = 2000; // time to display the splash screen in ms

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.splashscreen);

      Thread splashTread = new Thread() {
         @Override
         public void run() {
            try {
               int waited = 0;
               while (_active && (waited < splashTime)) {
                  sleep(100);
                  if (_active) {
                     waited += 100;
                  }
               }
            } catch (Exception e) {

            } finally {

               startActivity(new Intent(SplashScreen.this,
                     MainActivity.class));
               finish();
            }
         };
             };
      splashTread.start();
   }
}



You can also show splash screen until the file or image is downloaded from web using AsyncTask



Code: Select all

public class SplashScreen extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
 
      //show splash screen until the data is fetched from website
        new PrefetchData().execute();
 
    }
 
    /**
     * Async Task to download/fetch data from web
     */
    private class PrefetchData extends AsyncTask<Void, Void, Void> {
 
        @Override
        protected void onPreExecute() {
            super.onPreExecute();   
        }
 
        @Override
        protected Void doInBackground(Void... arg0) {
         //download the file or parse JSON or XML here
            return null;
        }
 
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }
}

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron