Thomas
Thomas

Reputation: 648

How to show ProgressBar after a certain amount of time

I am working with Android API level 23 (Android 6). I am using com.loopj.android.http.AsyncHttpClient to implement asynchronous communication with my backend server and it works fine.

While communicating with the server, I am showing a progress bar like this:

findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);

and hide it like this:

findViewById(R.id.loadingPanel).setVisibility(View.GONE);

when done as suggested here.

It is working really nicely.

Now most of the time, the requests to the server only take some milliseconds, so that showing and hiding the progress bar causes some flickering which is not necessary and annoying if the request time is only that short.

What would be a good way to show the progress bar only after a certain amount of time after the request started, lets say after one second?

Upvotes: 1

Views: 1333

Answers (4)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29794

You can simply use a CountDownTimer in a second tick. Like this:

 private boolean mNeedToCloseProgress = false;
 // in second with 100 ms tick
 CountDownTimer mCdtStartProgress = new CountDownTimer(1000, 1000) {

     public void onTick(long millisUntilFinished) {}

     public void onFinish() {
        mNeedToCloseProgress = true;
        findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
     }
  };

Then when you start the api call, you need to start the timer with:

mCdtStartProgress.start();

which will modify the flag mNeedToCloseProgresswhich telling whether the progress need to be close.

Then cancel the timer when you've finish calling the api and getting the result with:

mCdtStartProgress.cancel();
if(mNeedToCloseProgress) findViewById(R.id.loadingPanel).setVisibility(View.GONE);

Upvotes: 0

Arnab Banerjee
Arnab Banerjee

Reputation: 165

You could use a boolean flag variable initially set at true. Then when you receive the response from server you can flip the flag value. Then you can use the handler like:

Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        if(flag)
            findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);            
    }
}, 1000);

Handler endHandler = new Handler();
    endHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
                findViewById(R.id.loadingPanel).setVisibility(View.GONE);            
        }
    }, MAX_WAIT_TIME_YOU_WANT);

Upvotes: 0

Mithun Sarker
Mithun Sarker

Reputation: 4023

I am guessing you did something like following

 AsyncHttpClient client = new AsyncHttpClient();
 client.get("https://www.example.com", new AsyncHttpResponseHandler() {

    @Override
    public void onStart() {
        // called before request is started
       findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        // called when response HTTP status is "200 OK"
        findViewById(R.id.loadingPanel).setVisibility(View.GONE);
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
        // called when response HTTP status is "4XX" (eg. 401, 403, 404)
    }

    @Override
    public void onRetry(int retryNo) {
        // called when request is retried
    }
});

Just try something like following

boolean showProgress= true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AsyncHttpClient client = new AsyncHttpClient();
    client.get("https://www.google.com", new AsyncHttpResponseHandler() {

        @Override
        public void onStart() {
            // called before request is started
            //findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(showProgress){
                        findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
                    }
                }
            }, 5*1000); // your progress will start after 5 seconds
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
            // called when response HTTP status is "200 OK"
            //findViewById(R.id.loadingPanel).setVisibility(View.GONE);
            showProgress = false;
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
            // called when response HTTP status is "4XX" (eg. 401, 403, 404)
            showProgress = false;
        }

        @Override
        public void onRetry(int retryNo) {
            // called when request is retried
        }
    });
}

Upvotes: 2

Kévin Giacomino
Kévin Giacomino

Reputation: 487

You can set an delay before showing progress bar like this :

Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Do something after 1s = 1000ms
        findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
    }
}, 1000);

Upvotes: 0

Related Questions