How to implement a Splash Screen in Android ?
Implementing a splash screen in Android is a redundant question among beginners. With that tutorial, we’re going to see that it’s very easy to implement a splash screen with just some lines of code.
Before to continue, it’s important to understand that splash screen is not a recommended design pattern in Android. However, you can implement if you want and think that is a plus for your application.
First, we need a splash screen image.
Now, we need to define a first activity that will display the splash screen image during some time. UI is very simple : you can use for example a LinearLayout what matches the screen in width and height and then define our splash screen image as background.
In the activity, we define a constant SPLASH_TIME_OUT storing the time for the splash screen display. Then, main idea is to use a Handler and to post a Runnable in delayed time that corresponds to SPLASH_TIME_OUT.
In the run method of your Runnable, you launch intent to go on main activity of your application :
public class SplashScreen extends Activity { // Splash screen timer private static int SPLASH_TIME_OUT = 3000; @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); } }
To test the final result, go on that Youtube video and see the live coding of the splash screen demo application :
Leave a Reply
You must be logged in to post a comment.