Android: an easy splash screen implementation using ImageView

I’ve seen a number of suggestions of how to implement a splash screen properly. Most typical implementation is through activities. To show it, you need to make it the first activity in your application, which has a few disadvantages:

  • It is an activity, so it will use the Android-specific effect (typically sliding) to switch from splash screen to main menu, giving you little to no control over how you want your splash screen to disappear;
  • If you have an option to disable splash screen, the first activity would still show (since it is in manifest), and you’d have to close it and switch to main activity, which will have undesirable UI effects.

A much better way is to implement it as a view inside the same main activity. This would avoid both issues above.

A possible implementation is following:

   public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        if ( IS_SPLASH_SCREEN_ENABLED
        && (savedInstanceState == null 
             || !savedInstanceState.containsKey( "splashShown" ) ) )
        {
            // Show splash screen using the image view
            ImageView splashScreen = new ImageView(this);

            // Load the splash screen bitmap into the view - keep it light!
            splashScreen.setImageBitmap(
                       BitmapFactory.decodeResource(getResources(), 
                                      R.drawable.splash_screen_bitmap) );

            // and set it as main view
            setContentView( splashScreen );

            // This handler would close the splash screen once the time is
            // out, and will show the main UI instead.
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    showMainUI();
                }
            }, 5000); // timeout, in this case 5 seconds
        }
        else
            showMainUI(); // splash screen is disabled or has been shown
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState)
    {
        // Remember that we have shown the splash screen already
        outState.putBoolean( "splashShown", true );
        super.onSaveInstanceState(outState);
    }

    private void showMainUI()
    {
        // Show the main UI - this erases the old UI, 
        // including the splashscreen
        setContentView(R.layout.mainui_view);
This entry was posted in android.

Comments are closed.