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!
Step 1: Create an Android Project with empty Activity. Step 2: Create a Fragment and Open your java and XML file add these codes. Step 3: Add this code to your Fragment XML file Source Code. Step 4: Add these code In your activity button click whare open DialogFragment.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.
This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With