I have this Android activity/layout with a button. When I click this button, it opens a DialogFragment with 2 spinners. When the DialogFragment shows up, I need to populate these 2 spinners with items returned by a web service. So, while waiting for the service to return, I'd like to display a ProgressDialog over the DialogFragment I've just opened.
The problem is I can't seem to make the ProgressDialog on top of the DialogFragment. I can see the ProgressDialog being shown behind the DialogFragment, is it possible to make the ProgressDialog being displayed over everything else?
My code is like this:
public class RegionalFragment extends DialogFragment {
private View view;
private Activity activity;
private Spinner spinner1;
private Spinner spinner2;
private ProgressDialog progressDialog;
public RegionalFragment(Activity activity) {
this.activity = activity;
LayoutInflater inflater = activity.getLayoutInflater();
view = inflater.inflate(R.layout.spinner_selection, null);
this.progressDialog = new ProgressDialog(activity);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder =
new AlertDialog.Builder(activity)
.setTitle(R.string.title)
.setView(view)
.setPositiveButton(R.string.ok, new MyListener(activity))
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
getDialog().cancel();
}
});
progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(1));
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Please wait...");
progressDialog.show();
invokeAsyncWebService();
return builder.create();
}
Thanks!
Apparently thats a widely complained about subject with ProgressDialog
, I suggest you try a normal dialog, created with the builder and whose layout (a custom one made by you) includes the progress bar or loading or whatever you need. This should solve your problem.
I also had a problem when my parent DialogFragment was recreated due to configuration change. I couldn't make the progressDialog to be visible on top again. The solution was to show progressDialog in the onResume() event:
@Override
public void onResume() {
super.onResume();
if (inProgress) {
progressDialog = ProgressDialog.show(ctx, "Report request", "connecting...", true);
}
}
来源:https://stackoverflow.com/questions/22406223/how-can-i-open-a-progressdialog-on-top-of-a-dialogfragment