12

Whenever I want to show progress bar into my app, I call this method and this method adds ProgressBar into my layout.

Problem : I want to show this progress bar over Dialog, but Dialog is always shown above. What can be done for this situation?

public static void showProgressBar(@NonNull final Activity activity) {
    try {
        if (activity == null) {
            LogManager.printStackTrace(new NullActivityException());
            return;
        }
        View view = activity.findViewById(android.R.id.content);
        if (view == null) {
            LogManager.printStackTrace(new NullPointerException("content view is null"));
            return;
        }
        View rootView = activity.findViewById(android.R.id.content).getRootView();
        if (rootView == null || !(rootView instanceof ViewGroup)) {
            LogManager.printStackTrace(new NullPointerException("rootView is null or not an instance of ViewGroup"));
            return;
        }
        final ViewGroup layout = (ViewGroup) rootView;

        final ProgressBar progressBar = new ProgressBar(activity);
        progressBar.setIndeterminate(true);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Drawable wrapDrawable = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
            DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(activity, R.color.colorAccent));
            progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
        }
        RelativeLayout.LayoutParams params = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        final RelativeLayout rl = new RelativeLayout(activity);
        rl.setBackgroundColor(ActivityCompat.getColor(activity, R.color.tc_hint_grey_alpha));
        rl.setClickable(true);
        rl.setTag("#$UniqueProgressBar");
        ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                120);
        rl.setGravity(Gravity.CENTER);
        rl.addView(progressBar, params2);
        LogManager.i("ProgressBar", "ProgressUtils.showProgressBar->called");
        layout.addView(rl, params);

        mRunnable = new Runnable() {
            @Override
            public void run() {
                LogManager.i("ProgressBar", "ProgressUtils.showProgressBar->120 secs timeout");
                hideProgressBar(activity);
            }
        };
        mHandler = new Handler();
        mHandler.postDelayed(mRunnable, 120000);

        LogManager.i("ProgressBar", "Added");
    } catch (Exception e) {
        LogManager.printStackTrace(e);
    }

}
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43

8 Answers8

9

Try something like this, it should work globally :

public static void showProgressBar(@NonNull final Activity activity) {
    try {
        if (activity == null) {
            LogManager.printStackTrace(new NullActivityException());
            return;
        }

        final WindowManager wm = (WindowManager) activity.getApplicationContext().getSystemService(Activity.WINDOW_SERVICE);

        final ProgressBar progressBar = new ProgressBar(activity);
        progressBar.setIndeterminate(true);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Drawable wrapDrawable = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
            DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(activity, R.color.colorAccent));
            progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
        }

        WindowManager.LayoutParams windowLayoutParams = new WindowManager.LayoutParams();
                windowLayoutParams.gravity = Gravity.CENTER;
                windowLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
                windowLayoutParams.token = activity.getWindow().getDecorView().getWindowToken();
                windowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        windowLayoutParams.height = LayoutParams.MATCH_PARENT;
        windowLayoutParams.width = LayoutParams.MATCH_PARENT;

        wm.addView(progressBar, windowLayoutParams);

        LogManager.i("ProgressBar", "ProgressUtils.showProgressBar->called");
        layout.addView(rl, params);

        mRunnable = new Runnable() {
            @Override
            public void run() {
                LogManager.i("ProgressBar", "ProgressUtils.showProgressBar->120 secs timeout");
                hideProgressBar(activity);
            }
        };
        mHandler = new Handler();
        mHandler.postDelayed(mRunnable, 120000);

        LogManager.i("ProgressBar", "Added");
    } catch (Exception e) {
        LogManager.printStackTrace(e);
    }

}
John
  • 1,304
  • 1
  • 9
  • 17
1

If you are using custom design layout for your dialog then, you can put your progress bar inside your progress dialogs custom layout and use it from there. Then it will get displayed above ur dialog when ur dialog gets displayed.

Parth Vora
  • 19
  • 4
  • But I need a global solution for all of my dialogs used in the project. It will be too much work if I add progress bars in their layouts. – Harish Gyanani Aug 28 '17 at 11:58
  • 1
    "Global solutions" are bad. Reusable solutions are good. "Too much work" is laziness, laziness is also bad. – artkoenig Aug 29 '17 at 09:07
  • Then you can create a base dialog Activity or Fragment that your custom AlertDialog has to extend in order to display a progress bar in all different types of dialog screens. – Parth Vora Aug 31 '17 at 10:52
1

Try to use dialog fragment for your dialog and inflate progress bar inside the layout of dialog fragment.For more info check this link.

https://developer.android.com/reference/android/app/DialogFragment.html

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
1

Try this

ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
                        "Loading. Please wait...", true);

This will give you a progress bar which can be used as per your requirement.

Avid
  • 171
  • 16
1

Android has a ProgressDialog with AlertDialog as base class, and adds a progress functionality:

  ProgressDialog progress = new ProgressDialog(this);
  progress.setMessage("Downloading Music");
  progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  progress.setIndeterminate(true);
  progress.setProgress(0);
  progress.show();

and increment:

  progress.setProgress( ... );

Check here for more.

RonTLV
  • 2,376
  • 2
  • 24
  • 38
1

One thing I can think of is having abstract Dialog Fragment or normal custom Dialog with progress bar inside the view. With that said, your class should enable you to attach the dialog or other view. In that way, every time you wanna show dialog, extend that class or use that class and provide the view.

Or

call progress dialog inside your dialog again. But I would prefer first one.

Poe Poe
  • 76
  • 1
  • 7
0
simpleProgressBar.setMax(100); 
simpleProgressBar.setProgress(50); 
Harshit Trivedi
  • 764
  • 9
  • 33
-2

Use the below function code for the ProgressBar:

public void Progressbar_Timer() {
    final ProgressDialog dialog = new ProgressDialog(AddEventImageLayout.this);
    dialog.setTitle("Placeing selected image...");
    dialog.setMessage("Please wait..");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.show();

    long delayInMillis = 10000;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            dialog.dismiss();
        }
    }, delayInMillis);
    check = 2;
}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53