Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Loading, please wait

Is there a standard "Loading, please wait" dialog I can use in Android development, when I invoke some AsyncTask (downloading some data from remote service for example)?

like image 320
nikib3ro Avatar asked Dec 30 '09 16:12

nikib3ro


3 Answers

You mean something like an indeterminate ProgressDialog?

Edit: i.e.

ProgressDialog dialog = ProgressDialog.show(context, "Loading", "Please wait...", true);

then call dialog.dismiss() when done.

like image 151
Mirko N. Avatar answered Oct 21 '22 03:10

Mirko N.


If you implement runnable as well as extending Activity then you could handle the code like this...

private ProgressDialog pDialog;

public void downloadData() {
    pDialog = ProgressDialog.show(this, "Downloading Data..", "Please wait", true,false);
    Thread thread = new Thread(this);
    thread.start();
}

public void run() {
// add downloading code here
    handler.sendEmptyMessage(0);
 }

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        pDialog().dismiss();
        // handle the result here
    }
};

It's worth mentioning that you can set the content view of the progress dialog so you can display a custom message / image:)

pDialog.setContentView(R.layout.X); 
like image 33
Ally Avatar answered Oct 21 '22 03:10

Ally


Mirko is basically correct, however there are two things to note:

  1. ProgressDialog.show() is a shortcut that automatically creates a dialog. Unlike other dialogs, it should NOT be used in onCreateDialog(), as it will cause errors in Android 1.5.

  2. There are some further issues with AsyncTask + ProgressDialog + screen orientation changes that you should be aware of - check this out.

like image 37
Dan Lew Avatar answered Oct 21 '22 01:10

Dan Lew