Showing progress dialog within DialogFragment
Asked Answered
A

8

7

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?

Arana answered 24/4, 2014 at 14:39 Comment(0)
S
22

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;
    }
}
Storebought answered 24/4, 2014 at 18:8 Comment(5)
How is this supposed to work, if you call return super.onCreateDialog() ? You are not returning YOUR created progress dialog.Oleaceous
Thanks Yar, fixed the typo.Storebought
How would you access the dialog to report progress from AsyncTask.onProgressUpdate given a concrete ProgressDialogFragment frag?Hyperbole
You should add the ProgressDialogFragment to the FragmentManager with a tag and the call FragmentManager.findFragmentByTag to locate it again.Storebought
The most important part is, how to use it, I ask as beginner android developer.Hulburt
E
5

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.

Erasure answered 6/4, 2015 at 8:52 Comment(3)
The problem is when you try to dismiss dialog on activity recreation. The old DialogFragment is already destroyed, the new one is not created yet. So what is reliable way of dismissing a dialogfragment?Turtleback
@Erasure , why it is not a good practice to show a dialog fragment for async operations such as networking ?Rainy
because when you block ui with a dialog fragment, user cannot navigate to other screen or go back or do stg else and waits till your network operation end.Erasure
I
2

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.

Incumbency answered 24/4, 2014 at 14:58 Comment(0)
H
2

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();
}
Hammerskjold answered 3/10, 2017 at 9:48 Comment(0)
O
1

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();
Outrage answered 24/4, 2014 at 14:45 Comment(0)
M
1

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.

Mcdade answered 21/4, 2015 at 11:20 Comment(0)
B
0

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 {...}
Bors answered 18/9, 2018 at 9:55 Comment(0)
M
0

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() }
}
Memoried answered 17/2 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.