Question of using ContentLoadingProgressBar

android.support.v4.widget.ContentLoadingProgressBar is subclass of 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).

 I TRY to use it in this exercise. To display and hide it by calling its show() and hide() methods. By calling show(), show the progress view after waiting for a minimum delay. If during that time, hide() is called, the view is never made visible.

When long progress run after show(), the ContentLoadingProgressBar shown after a short time.
When short progress run after show(), the ContentLoadingProgressBar will not shown.

But the problem is: once ContentLoadingProgressBar no shown once, it will not be shown again! I don’t is it normal behavior, or anything wrong.


Here is my exercise:

MainActivity.java

package com.blogspot.android_er.androidcontentloadingprogressbar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

Button btnStartShortProgress, btnStartLongProgress;
ProgressBar progressBar;
ContentLoadingProgressBar contentLoadingProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartShortProgress = (Button)findViewById(R.id.startshortprogress);
btnStartLongProgress = (Button)findViewById(R.id.startlongprogress);
progressBar = (ProgressBar)findViewById(R.id.ProgressBar);
contentLoadingProgressBar =
(ContentLoadingProgressBar)findViewById(R.id.ContentLoadingProgressBar);

btnStartShortProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartShortProgress.setEnabled(false);
btnStartLongProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(3);
}
});

btnStartLongProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartShortProgress.setEnabled(false);
btnStartLongProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(50);
}
});
}

public class MyAsyncTask extends AsyncTask<Integer, Integer, Void> {

@Override
protected Void doInBackground(Integer... integers) {
int i = integers[0];

while(i-- > 0){
SystemClock.sleep(100);
}

return null;
}

@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
contentLoadingProgressBar.show();
}

@Override
protected void onPostExecute(Void aVoid) {
progressBar.setVisibility(View.GONE);
contentLoadingProgressBar.hide();
btnStartShortProgress.setEnabled(true);
btnStartLongProgress.setEnabled(true);
}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidcontentloadingprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"
/>

<Button
android:id="@+id/startshortprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start SHORT Progress"/>
<Button
android:id="@+id/startlongprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start LONG Progress"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ProgressBar"
android:textStyle="bold"/>
<ProgressBar
android:id="@+id/ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ContentLoadingProgressBar"
android:textStyle="bold"/>
<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/ContentLoadingProgressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>

</LinearLayout>

Examples of ProgressBar and AsyncTask

Android-er