Add the loading screen in starting of the android application
Asked Answered
M

7

27

My app is loading the start page in 10 seconds. In that time of 10 sec android screen is blank. In that time I want to add the loading screen. How to add it? And tell me in app how to know the starting page is loading? And tell me how to do in my app?

Mizell answered 28/6, 2013 at 4:33 Comment(4)
u can use full screen dialog or u can use splash screen.when your process starts show the dialog and when it ends dismiss it.Beckham
I think this link will help you: https://mcmap.net/q/505032/-android-splash-screen. Good luck!Laszlo
Instead of a splashcreen, have you considered some fake data, o some tutorials, a 10 seconds splashscreen will loock your app and will be really frustrating for the user: cyrilmottier.com/2012/05/03/…Diligent
What is happening in that 10 seconds? Your app design might be wrong.Cavender
Y
27

You can use splash screen in your first loading Activity like this:

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

        Thread welcomeThread = new Thread() {

            @Override
            public void run() {
                try {
                    super.run();
                    sleep(10000);  //Delay of 10 seconds
                } catch (Exception e) {

                } finally {

                    Intent i = new Intent(SplashActivity.this,
                            MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        welcomeThread.start();
    }

Hope this code helps you.

Yolondayon answered 28/6, 2013 at 4:38 Comment(3)
lets say you opening this app, and after 5s you leaving it, then after another 5s the app shows again becouse it starts main activity. Dont forget to cancel this somehow when you leave app / rotate screen etc.Coelenterate
so all the processing due to which time is taken happens in that SplashActivity ? if yes then how to create it an enclosing class , as in I created a new class called SplashActivity but now the error says it that it is not an enclosing class...please help... Sorry Im new to androidCorrespondent
This is not entirely correct. It doesn't take into account the time needed to inflate layout of splash activity, which will result in (shorter) blank screen on startup. See @Calculating answer instead.Lezlielg
E
42

use ProgressDialog.

ProgressDialog dialog=new ProgressDialog(context);
dialog.setMessage("message");
dialog.setCancelable(false);
dialog.setInverseBackgroundForced(false);
dialog.show();

hide it whenever your UI is ready with data. call :

dialog.hide();
Eschar answered 28/6, 2013 at 4:55 Comment(2)
ps: it's generally better to dismiss() a dialogEmelda
ProgressDialog is now deprecated. See https://mcmap.net/q/142648/-best-way-to-show-a-loading-progress-indicatorVolvox
C
28

Please read this article

Chris Stewart wrote there:

Splash screens just waste your time, right? As an Android developer, when I see a splash screen, I know that some poor dev had to add a three-second delay to the code.

Then, I have to stare at some picture for three seconds until I can use the app. And I have to do this every time it’s launched. I know which app I opened. I know what it does. Just let me use it!

Splash Screens the Right Way

I believe that Google isn’t contradicting itself; the old advice and the new stand together. (That said, it’s still not a good idea to use a splash screen that wastes a user’s time. Please don’t do that.)

However, Android apps do take some amount of time to start up, especially on a cold start. There is a delay there that you may not be able to avoid. Instead of leaving a blank screen during this time, why not show the user something nice? This is the approach Google is advocating. Don’t waste the user’s time, but don’t show them a blank, unconfigured section of the app the first time they launch it, either.

If you look at recent updates to Google apps, you’ll see appropriate uses of the splash screen. Take a look at the YouTube app, for example.

enter image description here

Calculating answered 22/2, 2017 at 14:50 Comment(3)
This should be accepted answer. Everything else mentioned here is garbage..Lezlielg
It didnt worked dont know why causing issues in gradle buildIsoniazid
In-lining the code snippets from the article would be helpfulHooknose
Y
27

You can use splash screen in your first loading Activity like this:

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

        Thread welcomeThread = new Thread() {

            @Override
            public void run() {
                try {
                    super.run();
                    sleep(10000);  //Delay of 10 seconds
                } catch (Exception e) {

                } finally {

                    Intent i = new Intent(SplashActivity.this,
                            MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        welcomeThread.start();
    }

Hope this code helps you.

Yolondayon answered 28/6, 2013 at 4:38 Comment(3)
lets say you opening this app, and after 5s you leaving it, then after another 5s the app shows again becouse it starts main activity. Dont forget to cancel this somehow when you leave app / rotate screen etc.Coelenterate
so all the processing due to which time is taken happens in that SplashActivity ? if yes then how to create it an enclosing class , as in I created a new class called SplashActivity but now the error says it that it is not an enclosing class...please help... Sorry Im new to androidCorrespondent
This is not entirely correct. It doesn't take into account the time needed to inflate layout of splash activity, which will result in (shorter) blank screen on startup. See @Calculating answer instead.Lezlielg
M
3

You can create a custom loading screen instead of splash screen. if you show a splash screen for 10 sec, it's not a good idea for user experience. So it's better to add a custom loading screen. For a custom loading screen you may need some different images to make that feel like a gif. after that add the images in the res folder and make a class like this :-

public class LoadingScreen {private ImageView loading;

LoadingScreen(ImageView loading) {
    this.loading = loading;
}

public void setLoadScreen(){
    final Integer[] loadingImages = {R.mipmap.loading_1, R.mipmap.loading_2, R.mipmap.loading_3, R.mipmap.loading_4};
    final Handler loadingHandler = new Handler();
    Runnable runnable = new Runnable() {
        int loadingImgIndex = 0;
        public void run() {
            loading.setImageResource(loadingImages[loadingImgIndex]);
            loadingImgIndex++;
            if (loadingImgIndex >= loadingImages.length)
                loadingImgIndex = 0;
            loadingHandler.postDelayed(this, 500);
        }
    };
    loadingHandler.postDelayed(runnable, 500);
}}

In your MainActivity, you can pass a to the LoadingScreen class like this :-

private ImageView loadingImage;

Don't forget to add an ImageView in activity_main. After that call the LoadingScreen class like this;

LoadingScreen loadingscreen = new LoadingScreen(loadingImage);
loadingscreen.setLoadScreen();

I hope this will help you

Manmade answered 27/12, 2016 at 10:21 Comment(0)
B
2
public class Splash extends Activity {

    private final int SPLASH_DISPLAY_LENGHT = 3000;            //set your time here......

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

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(Splash.this,MainActivity.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}
Bluecollar answered 28/6, 2013 at 5:36 Comment(0)
F
1

If the application is not doing anything in that 10 seconds, this will form a bad design only to make the user wait for 10 seconds doing nothing.

If there is something going on in that, or if you wish to implement 10 seconds delay splash screen,Here is the Code :

ProgressDialog pd;
pd = ProgressDialog.show(this,"Please Wait...", "Loading Application..", false, true);
pd.setCanceledOnTouchOutside(false);
Thread t = new Thread()
{ 
      @Override
      public void run()
      {
                try
                {
                    sleep(10000)  //Delay of 10 seconds
                } 
        catch (Exception e) {}
        handler.sendEmptyMessage(0);
        }
} ;
t.start();

//Handles the thread result of the Backup being executed.
private Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg) 
    {
        pd.dismiss();
        //Start the Next Activity here...

    }
};
Fretted answered 28/6, 2013 at 6:40 Comment(0)
W
0

Write the code:

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

        Thread welcomeThread = new Thread() {

            @Override
            public void run() {
                try {
                    super.run();
                    sleep(10000)  //Delay of 10 seconds
                } catch (Exception e) {

                } finally {

                    Intent i = new Intent(SplashActivity.this,
                            MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        welcomeThread.start();
    }
Wizardry answered 14/9, 2016 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.