How to show loading message or dialog until the other activity loading because it take long time -android-

366 Views Asked by At

i have activity take long time until get loading so i want when i click start this activity to show loading message like ( loading..... )

here is the button that started this activity

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(test.this, MainActivity.class);
            startActivity(intent);

        }
    });

so what the code that should i write here to show this loading message until the MainActivity load?

2

There are 2 best solutions below

0
daneejela On

If you have activity A that on click shows activity B that takes long time to load, the most common way is this:

  • in the activity B layout view have a progress bar and make it visible in onCreate of activity B

  • all long running operations put either in a asyncTask or in some Service that would run in the background

  • when long running operation finishes - update progress bar by setting it's visibility to View.GONE.

I would write you a code, but since I don't have your code, I think the best way I could help is to break your problem into a steps.

0
Basic Coder On

daneejela provided a solution but I do think that the reason for you long startup of your MainActivity is the real problem and should get fixed.

Activities will take long to startup when you do too much block method calls or such things in the MainThread. Try to do those things in a background Thread (you can use AsyncTask). This will result in an immediate Activity startup. While your AsyncTask is working, you can show a little ProgressDialog or better, show somewhere in your Activity a ProgressBar which is not blocking the user from doing any kind of action.

You should definitly understand Android's Main- and BackgroundThreading. There are also great tutorials for Dialogs. You should also check out the Android Design Guidlines.