Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open a ProgressDialog on top of a DialogFragment?

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!

like image 824
felipecao Avatar asked Mar 14 '14 13:03

felipecao


People also ask

How do I open DialogFragment?

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.

How do I get view from AlertDialog?

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.

How do you finish DialogFragment?

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.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.


1 Answers

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.

like image 70
Juan Cortés Avatar answered Sep 18 '22 08:09

Juan Cortés