this is a way:
public void showProgressDialog() {
runOnUiThread(new Runnable() {
public void run() {
mProgressDialog = onCreateProgressDialog();
mProgressDialog.show();
}
});
}
private ProgressDialog onCreateProgressDialog() {
ProgressDialog dialog = new ProgressDialog(RequesterActivity.this);
dialog.setTitle(getString(R.string.label_dialog_item_to_find) +" "+
((EditText) findViewById(R.id.edit_text_item_id)).getText().toString());
dialog.setMessage(getString(R.string.label_dialog_finding_item));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
return dialog;
}
Note that you could give a "no Style" to the dialog like in here Dialog with transparent background in Android or here android dialog transparent depending on what you need.
I on the other hand use the ProgressDialog
with some info in it like "Please wait while loading item..."
. After you get the Data from the server just call the cancel()
method for the ProgressDialog
being showed (this ProgressDialog
should be created in an instance variable, so you keep a track of it and could call cancel()
from everywhere) then show whatever you will show with the Data you now have.
As a plus, since your download is a background process, you could use the Observer Pattern (Listener Pattern) where your Activity will implement a let's say AsyncTaskListener
interface where AsyncTask
is the name of the class of your download background process, this interface should have the methods that the Activity will call on the background process, let's say a method like cancelProgressDialog()
that will be called in the onPostExecute()
of your AsyncTask
class, the last thing you need to do is create an instance variable (member) of the interface in your AsyncTask class and maybe by the Constructor
or a Setter
method set the interface to it.
Here is an Example:
Interface:
public interface MessageHandlerListener {
public void toastMessage(int messageId);
}
Activity:
public class AnyActivityThatNeedsToShowAMessageInABackgroundProcessOrInUIThread extends Activity implements MessageHandlerListener {
public void toastMessage(int messageId){
//show a toast implementation
}
}
AsyncTask:
public class BackgroundProcess extends AsyncTask {
MessageHandlerListener mMessageHandlerListener;
public BackgroundProcess(MessageHandlerListener m){
mMessageHandlerListener = m;
}
protected Object doInBackground(Object[] objects){
return null
}
protected void onPostExecute(Object o) {
mMessageHandlerListener.toastMessage(Message.CONSTANT_MESSAGE);
}
}
This is a way better aproach and let you apply the MVC pattern in android where your background process could be the Controller, your Activity(s) the View and have a good Model depending on the needs of the project. Regards.