11

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?

inazaruk
  • 74,247
  • 24
  • 188
  • 156
Paul
  • 2,465
  • 8
  • 35
  • 60

3 Answers3

9

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!

Srichand Yella
  • 4,218
  • 2
  • 23
  • 24
  • 2
    But please use `Handler` instead of `TimerTask`. – inazaruk Jun 19 '11 at 17:44
  • Did you test this code on emulator or real device? There's NO `Timer` on Android! – Luke Vo Jun 20 '11 at 02:16
  • 2
    Timer is not an android object, its a java object. It is a working code in one of my apps. It works on both a device and an emulator. Add the following to your import list and you will be fine: import java.util.Timer; import java.util.TimerTask; – Srichand Yella Jun 20 '11 at 06:13
  • 1
    Delaying an app start is never a good policy. This [link](https://android.jlelse.eu/the-complete-android-splash-screen-guide-c7db82bce565) shows an article that explains how to properly do it. The core idea is to load an image while the app is doing the initial processing. – Laranjeiro Sep 28 '18 at 14:42
1

This start up image also known as 'splash screen'. Here you can find how to make splash screen.

Community
  • 1
  • 1
jamapag
  • 9,290
  • 4
  • 34
  • 28
0

Your needs is callign Splash Screen. Here is my splash screen code.

Just add new activity and set application for opening this activity.

public class SplashActivity extends DeviceInfoAbstractActivity {

@SuppressLint("MissingSuperCall")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, R.layout.activity_splash);

    passScreen();
}

private void passScreen() {

    new CountDownTimer(1000, 2000) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {

            Intent intent = RDAIntentHelpers.getClearCacheIntent();

            intent.setClass(SplashActivity.this, MainActivity.class);

            startActivity(intent);

        }
    }.start();
}

@Override
public void onBackPressed() {
    //no exit
}
}

and this my getClearCacheIntent() method

public static Intent getClearCacheIntent() {

    Intent intent = new Intent();

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    return intent;
}

after these, your splash screen stays on screen for 2 seconds. Do whatever you want =)

Arda Kaplan
  • 1,720
  • 1
  • 15
  • 23