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?
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;
}
}
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.
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