Show Splash Screen for fixed amount of time in Android Apps


You'll be into situations where you have to present a user with a splash screen that stays for sometime and then loads the main application and goes away.

This post demonstrates a code that displays a splash screen for a fixed amount of time and then loads the main screen.

This code isn't for the situation where you have to display a splash screen until you finish doing some work or load data from the web.
It will be covered in a different post.

Read the code, and understand the things from the comments

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;

public class SplashScreen extends Activity {

private final int DISPLAY_LENGTH = 1000;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash_screen);

/* New Handler to start the Menu-Activity
* and close this Splash-Screen after DISPLAY_LENGTH/1000 seconds.*/
new Handler().postDelayed(new Runnable(){

@Override
public void run() {
/* Create an Intent that will start the Main-Activity. */
Intent mainIntent = new Intent(SplashScreen.this,Main.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
}}, DISPLAY_LENGTH);
}
}

3 comments:

chitgoks said...

hi. i tried to use this but after the splash screen, instead of going to my mainactivity class, it says it stopped unexpectedly.

the android manifest file should point to the splashscreen class right? instead of the main class since the splashscreen is the activity class that needs to be run first

Aman Alam said...

Sorry, I am seeing this comment today :(

Yes, manifest should point to the Splash Screen instead.
However, having a Splash Screen is now a bad practice, but could you tell the exact error? from the logcat?

Shubham Singh said...

Very nice dude...

I have also found one good link here....

Splash screen - Android Example