Is there a general way to display a progress bar over Views in Android?

I've seen numerous posts on how to display a progress bar while data is loading in the background. All suggestions require that we manually place the ProgressBar in the xml layout and then use AsyncTask

to show and hide the ProgressBar and View in the question.

However, I would like to create a generic class that programmatically creates a ProgressBar at runtime and positions it exactly over the view in question, and can also tint or blur the view slightly while the ProgressBar is displayed. If it was a Swing application, I would draw my progress bar on glass pane , shading it slightly with gray. In this case, since the progress bar is a child of the same pseudo parent, so I could easily position this as centered.

In the Android UI toolbox, I don't know of such glass panes. How do you achieve this effect?

+2


source to share


3 answers


Create a BaseActivity from which you get all your Activities (same as for Fragments).

Give him something like

protected void showLoading(){

  ViewGroup content = findViewById(...); 
  content.setVisibility(Visibility.GONE); 

  ViewGroup root = findViewById(...); 
  root.addView(new ProgressBar()); 

}

      



Be sure to make sure all your layouts have a ViewGroup for root and one for content, which might not otherwise be needed and bloat layouts, but this is how I do it and everything works fine. The above is of course pseudocode, but you get the idea.

There's also this library there: http://www.androidviews.net/2013/04/progressfragment/ but I don't find it necessary to import the library for this task.

0


source


Unfortunately, you need to create this functionality. I always do this by creating a class from a framelayout and then positioning my image inside using my progressbar ontop. Then I create an interface that I use as a callback, so that when the specified process is complete and the data is finished, I get my callback and I hide the progress bar. I use framelayout because it is the simplest view that can be used to "fold" views in one place by simply placing them in a FrameLayout. You may also need to place the views inside a frame inside a relativelayout with the width and height set to match the parent so you can set layout_centerInParent to true on the progress bar so that it sits nicely inside your complex view.



0


source


Ok, I'm not sure if I'm asking the question correctly because it seems easier to me than it can be. But anyway:

To create a progress bar programmatically, you need to do the following in your activity:

    ProgressBar pb = new ProgressBar(this);
    ((ViewGroup) this.findViewById(R.id.view_that_will_contain_progressbar)).addView(pb);

      

This will add the view to the ViewGroup view_that_will_contain_progressbar

. This ViewGroup is a must FrameLayout

if you want to overlay on top of other information.

Council. If you want to customize the ProgressBar, you can declare it in your layout file and do the following to create and attach a PB (still in your activity / fragment):

this.getLayoutInflater().inflate(R.layout.progressbar,parent);

with a parent referencing the parent you want to bind it to.

0


source







All Articles