0

Is there a preferred way to show 'loading' animations in Android? Currently I'm showing Dialog with the text "Loading..." for long running processes. It's tricky to get a .gif to work for this, so I'm wondering if this is a problem that has been solved before, and if so, how?

DaveDev
  • 41,155
  • 72
  • 223
  • 385

5 Answers5

3

In my apps, I typically use a ProgressDialog to show a spinning "Loading..." message. There are fancier/prettier ways, but this is a quick and easy built-in solution.

ProgressDialog progressDialog = ProgressDialog.show(Activity.this, "",
                        "Loading...");

...Do some work...

progressDialog.dismiss();

progressDialog = null;

If you have a static Utilities class to generate things like Alerts and Dialogs, here are 2 nice additions:

public static ProgressDialog createProgressDialog(Context context, String title, String message, boolean isCancelable)
{
    ProgressDialog dialog = new ProgressDialog(context);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setCancelable(isCancelable);

    return dialog;
}

If you are using AsyncTasks to do your work in the background, it is especially important to check that the Activity is still "alive" and not finishing when you show and dismiss the ProgressDialog, or your app will die with a bizarre exception(see this answer).

public static void safeShowProgressDialog(Context context, ProgressDialog dialog)
{
    if(!((Activity) context).isFinishing())
    {
        dialog.show();
    }
}

Same for dismissing the dialog:

public static void safeDismissProgressDialog(Context context, ProgressDialog dialog)
{
    if(!((Activity) context).isFinishing())
    {
        dialog.dismiss();
    }
}

See the API reference:

http://developer.android.com/reference/android/app/ProgressDialog.html

Community
  • 1
  • 1
DiscDev
  • 38,652
  • 20
  • 117
  • 133
  • To follow Android guidelines, you should not display the "loading" message : http://developer.android.com/design/building-blocks/progress.html#activity – Oyashiro Aug 14 '13 at 15:22
  • @Oyashiro - while your statement is true according to the Android docs, I believe that sometimes it is a good idea to show the user some text. For example, if you have multiple "things" happening in the background that takes a while, it might be a good idea to let them know what's going on by updating the "message" the ProgressDialog is showing. Of course, this is totally up to the designer of the app, but if I were using an app that allowed me to select a bunch of photos, create a zip file, then upload it, I'd like to know if it's "Zipping files...", or "Uploading...". Maybe it's just me :) – DiscDev Aug 14 '13 at 15:26
1

Here is good example:

Let's say you try to login and wait response from Server. On wait you show Progress Dialog and on Success switch login activity to Main:

    private ProgressDialog dialog;

   ...
 dialog = ProgressDialog.show(FirstLoginActivity.this, "", "Connecting. Please wait...", true);
    HeavyTask task = new HeavyTask();
    task.execute(str1, str2);   

private class HeavyTask extends AsyncTask<String, Void, Void> {

    private String str1= "";
    private String str2= "";



    protected Void doInBackground(String... args) {

        str1= args[0];
        str2= args[1];


        try {
            doSomething(str1, str2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(Void results) {

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                doStuff(str1, str2);                    
            }
        }, 500);            
    }   

private void doStuff(String str1, String str2) {
  mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {

                                startActivity(new Intent().setClass(FirstLoginActivity.this, OtherActivity.class).setData(getIntent().getData()));
                                Toast.makeText(getApplicationContext(), "Registration succeeded.", Toast.LENGTH_LONG).show();
                                //kill the dialog waiting
                                dialog.dismiss();
                                dialog = null;


                            }
                        }, 1000);

}
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
0

You can find some guidance in the Android Design documentation: http://developer.android.com/design/building-blocks/progress.html

Eric Levine
  • 13,536
  • 5
  • 49
  • 49
0

you can use progress bar. i usually use it through asyctasks. and overload the following method as:

protected void onProgressUpdate(Integer... progress) {
((ProgressBar) findViewById(R.id.progressBar1)).setProgress(progress[0]);
}
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
0

Depending on your application design it would vary but a ProgressDialog is a fairly common solution. This tutorial would show you how it all get's tied together and the two variations (with or without displaying progress).

JRomero
  • 4,878
  • 1
  • 27
  • 49