I am trying to show a progress dialog from a custom dialog while an image get uploaded to a server. Here is what I have:
final Dialog mDialog = new Dialog(this, R.style.CustomDialogTheme);
mDialog.setContentView(R.layout.sendimage_dialog);
ImageView im = (ImageView)mDialog.findViewById(R.id.image_to_upload);
im.setImageURI(Constants.currImageURI);
Button upload = (Button)mDialog.findViewById(R.id.upload);
upload.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
mDialog.dismiss();
});
mDialog.show();
mDialog.setOnDismissListener(new OnDismissListener(){
@Override
public void onDismiss(DialogInterface di) {
ProgressDialog progress = new ProgressDialog(CameraActivity.this, R.style.CustomDialogTheme);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setMessage("Please wait while your photo is being uploaded.");
progress.show();
String response = uploadUserPhoto(new File(getRealPathFromURI(Constants.currImageURI)));
try {
parseResponse(response);
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
e.printStackTrace();
}
progress.dismiss();
Toast.makeText(getApplicationContext(), "Using your computer, log into your graFighters account and your image will be waiting for you.", Toast.LENGTH_SHORT).show();
}
});
The problem is, when I hit the upload button, my progress dialog never shows. Any ideas why this might be?
It is because you’re performing the network call (uploadUserPhoto) in the main (UI) thread, which means that the UI won’t be updated between the calls to show() and dismiss(). If you want this to work, you could launch a separate worker Thread that performs the uploadUserPhoto instead. After the worker Thread has finished, it must then signal the Activity to remove the dialog.