Image on start up / loading

Paul picture Paul · Jun 19, 2011 · Viewed 16.5k times · Source

I ma developing an app, which at the moment when it is loading from the onCreate point, I just have a black screen (until the app gets its footing). Looking at other apps they have a company logo or cool image that pops up for a few seconds, can someone tell me how to do this please?

And if you can set it to display for a minimal time?

Answer

Srichand Yella picture Srichand Yella · Jun 19, 2011

Create a new activity that displays the image for a few seconds and redirects to your main activity:

public class SplashActivity extends Activity
{
    private static final long DELAY = 3000;
    private boolean scheduled = false;
    private Timer splashTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        splashTimer = new Timer();
        splashTimer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                SplashActivity.this.finish();
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
         }, DELAY);
       scheduled = true;
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (scheduled)
            splashTimer.cancel();
        splashTimer.purge();
    }
}

Set your image as the background for this activity. Hope that helps. Good luck!