62

I was going through developer site of android and I found a class named ContentLoadingProgressBar. By seeing this class I come up with some questions in my mind. It would be great if someone answers my questions.

  1. What is the difference between Normal ProgressBar and ContentLoadingProgressbar?
  2. What is the practical usage of ContentLoadingProgressBar?
  3. Can we show/hide this progressbar according to our requirement?

  4. How can I custom style this progressBar?

Thanks for your help in Advance. It would be great if someone explain it using codes and examples. Thank you.

Charuක
  • 12,953
  • 5
  • 50
  • 88
swetabh suman
  • 1,949
  • 2
  • 19
  • 24
  • Similar: https://stackoverflow.com/questions/20438751/example-usage-for-contentloadingprogressbar – Pang Jan 10 '23 at 08:28

4 Answers4

72

Here are my answers!

What is the difference between Normal ProgressBar and ContentLoadingProgressbar?

ContentLoadingProgressbar waits a minimum time to be dismissed before showing using hide() like with-in 0.5 seconds.So even the show() gets called this can be dismissed before it appear on the screen.

What is the practical usage of ContentLoadingProgressBar

It prevents very fast flickering stuff that you might see with "naive" implementations.

Can we show/hide this progressbar according to our requirement

Yes

How can I custom style this progressBar

<android.support.v4.widget.ContentLoadingProgressBar
        android:id="@+id/address_looking_up"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:visibility="visible" />

replace style with android:theme https://stackoverflow.com/a/38282149/5188159

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
15

Let's say you want to show a ProgressBar for some background operation that may take less than 500ms or more than 5 seconds.

  1. You call progressBar.show() Then you start your background operation.

  2. If your background operation is over within 500ms. Then you call progressBar.hide()

    Now the user will see a flicker of the progress bar appearing and disappearing within a fraction of a second.

Introducing ContentLoadingProgressBar:

When you use this progressbar, then it will wait for a minimum time before showing the progress dialog. Which means if the time between show() call and hide() call is less than that minimum time, then there won't be any dialog to be shown to the user.

Pang
  • 9,564
  • 146
  • 81
  • 122
amalBit
  • 12,041
  • 6
  • 77
  • 94
12

According to the docs:

ContentLoadingProgressBar implements a ProgressBar that waits a minimum time to be dismissed before showing. Once visible, the progress bar will be visible for a minimum amount of time to avoid "flashes" in the UI when an event could take a largely variable time to complete (from none, to a user perceivable amount)

This clearly mentions it's no different from a regular ProgressBar . Furthermore, this is a UI tweak to be precise, i.e. ContentLoadingProgressBar wouldn't show up if hide() is called in less than 0.5s after executing show(), thus preventing from quick flickering in the UI.

Pang
  • 9,564
  • 146
  • 81
  • 122
OBX
  • 6,044
  • 7
  • 33
  • 77
0

One note of using ContentLoadingProgressBar for recyclerview items. I have a scenario in which an arbitrary RV item can download something on clicked and shows indeterminate progress until completion. It appears to be impossible to use the benefits of CLPB in such case because of maintaining internal delays in CLPB when show()/hide() it: reused views may have inconsisted progress state (progress disappears or become infinite depending on reused holder view state). Thus i was forced to return to old good setVisibility:

public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position)
               ...
        if (item.isLoading()) {
        //holder.progressBar.show();
        holder.progressBar.setVisibility(View.VISIBLE);
    } else {
        //holder.progressBar.hide();
        holder.progressBar.setVisibility(View.INVISIBLE);
    }
RoK
  • 960
  • 1
  • 7
  • 6