deimos1988
deimos1988

Reputation: 6086

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?

Upvotes: 7

Views: 36103

Answers (8)

MeLean
MeLean

Reputation: 3421

I do it that way:

This is the class with the dialog fragment:

class FullscreenLoadingDialog : DialogFragment() {
    private lateinit var binding: DiaogLoadingBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = DiaogLoadingBinding.inflate(layoutInflater)
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            setBackgroundDrawableResource(android.R.color.transparent)
            setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        }
    }

    companion object {
        const val TAG = "FullscreenLoadingDialog"
    }

}

the XML view diaog_loading.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progressIndicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:indeterminate="true"
        android:indeterminateTint="@color/purple_200" />

</FrameLayout>

Then you could show/hide from anywhere that you have access to a FragmentManager. I made two Kotlin extension functions:

showing like this:

fun FragmentManager.showLoading() {
    // Check if the dialog is already added or visible
    val existingDialog = findFragmentByTag(FullscreenLoadingDialog.TAG) as? FullscreenLoadingDialog
    if (existingDialog == null || !existingDialog.isAdded) {
        // Dialog is not added yet, now we can show it
        FullscreenLoadingDialog().show(this, FullscreenLoadingDialog.TAG)
    }
}

hiding like this:

fun FragmentManager.hideLoading() {
    (findFragmentByTag(FullscreenLoadingDialog.TAG) as? FullscreenLoadingDialog)
        ?.run { dismiss() }
}

Upvotes: 0

Mahbubur Rahman Khan
Mahbubur Rahman Khan

Reputation: 415

Just use this class as progress dialog....

package com.microfinance.app.microfinance;

import android.app.ProgressDialog;
import android.support.annotation.VisibleForTesting;
import android.support.v4.app.Fragment;

/**
 * Created by USER on 9/18/2018.
 */

public class BaseFragment extends Fragment {
    @VisibleForTesting
    public ProgressDialog mProgressDialog;

    public void showProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this.getContext());
            mProgressDialog.setMessage("Loading ...");
            mProgressDialog.setIndeterminate(true);
        }

        mProgressDialog.show();
    }


    public void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        hideProgressDialog();
    }
}

Uses....

public class DialogFragment extends BaseFragment {...}

Upvotes: 0

FinalDark
FinalDark

Reputation: 1748

Show the progressDialog in onStart method inside DialogFragment class

@Override
public void onStart() {
    super.onStart();
    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();
}

Upvotes: 2

Himanshu Narang
Himanshu Narang

Reputation: 131

I know it is a very old post, but I want to tell a solution. If I am not wrong you are trying to show your progress dialog before onStart lifecycle method. Show progress dialog in onResume method.

Upvotes: 1

savepopulation
savepopulation

Reputation: 11921

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.

Upvotes: 5

Simon
Simon

Reputation: 11190

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

Upvotes: 22

mike20132013
mike20132013

Reputation: 5425

Try this :

ProgressDialog prog = (ProgressDialog)your_view.findViewById(R.id.yourprogress_id);
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();

Or

If dynamically using ProgressDialog;

ProgressDialog prog= new ProgressDialog(getActivity());//Assuming that you are using fragments.
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();

On Data loaded,

prog.dismiss();

Now, if the progress bar is not showing inside your dialog, you will have to set a custom dialog and define your progress bar inside that custom dialog.

Upvotes: 2

SacreDeveloper
SacreDeveloper

Reputation: 1251

Use ProgressBar inside your dialog fragment. add progressBar view in your xml code .

In Your code:

ProgressBar progressBar = view.findViewById(R.id.progress);
progressBar .show();

when finish:

 progressBar.hide();

Upvotes: 1

Related Questions