Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing progress dialog within DialogFragment

I would like to show a progress dialog within a dialog fragment.

However when I am using this code

ProgressDialog prog = new ProgressDialog(ctx);
prog.setTitle(getString(R.string.pleaseWait));
prog.setMessage(getString(R.string.webpage_being_loaded));       
prog.setCancelable(false);
prog.setIndeterminate(true);
prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
prog.show();

the progress dialog is shown in the fragment that called the dialogFragment, not in the DialogFragment itself.

What did I do wrong?

like image 484
deimos1988 Avatar asked Apr 24 '14 14:04

deimos1988


2 Answers

Here you go.

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;


public class ProgressDialogFragment extends DialogFragment
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setCancelable(false);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
        dialog.setTitle(getString(R.string.pleaseWait));
        dialog.setMessage(getString(R.string.webpage_being_loaded));
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        return dialog;
    }
}
like image 125
Simon Avatar answered Sep 24 '22 03:09

Simon


Here's a simple loading dialog fragment which plays a gif as a animation drawable

public class LoadingDialogFragment extends DialogFragment {
    public static final String FRAGMENT_TAG = "LoadingFragment";
    private ImageView ivLoading;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.dialog_loading, container, false);

        if (rootView != null) {
            ivLoading = (ImageView) rootView.findViewById(R.id.iv_loading);
            ivLoading.setBackgroundResource(R.drawable.anim_drawable);
        }
        return rootView;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            AnimationDrawable loadingAnimation = (AnimationDrawable) ivLoading.getBackground();
            loadingAnimation.start();
        }
    }
}

And you can show and hide it with:

 public void showLoadingDialog() {
      LoadingDialogFragment fragment = (LoadingDialogFragment) getSupportFragmentManager().findFragmentByTag(LoadingDialogFragment.FRAGMENT_TAG);
      if (fragment == null) {
         fragment = new LoadingDialogFragment();
         fragment.setCancelable(false);
         getSupportFragmentManager().beginTransaction()
                                    .add(fragment, LoadingDialogFragment.FRAGMENT_TAG)
                                    .commitAllowingStateLoss();

         // fragment.show(getSupportFragmentManager().beginTransaction(), LoadingDialogFragment.FRAGMENT_TAG);
      }
   }

   public void hideLoadingDialog() {
      LoadingDialogFragment fragment = (LoadingDialogFragment) getSupportFragmentManager().findFragmentByTag(LoadingDialogFragment.FRAGMENT_TAG);
      if (fragment != null) {
         // fragment.dismissAllowingStateLoss();
         getSupportFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
      }
   }

Edit: By the way it's not a good practice to show a dialog fragment for async operations such as networking.

like image 32
savepopulation Avatar answered Sep 22 '22 03:09

savepopulation