Mostafa Khaled
Mostafa Khaled

Reputation: 372

how to make a countdown progress bar going down continuously?

I am using a count down progress bar in android. I want it to be updated with the new time without stopping. It is now working with me but it stops for a short time then jump a part in the progress I don't want that I want it to be always decreasing without stopping.

This is my xml code :

<ProgressBar
    android:id="@+id/timerProgressbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_below="@id/appBar"
    android:layout_marginTop="-2dp"
    android:layout_marginLeft="-1dp"
    android:layout_marginRight="-1dp"
    android:max="20"
    android:rotation="180"
    android:progress="20" />

and this is the java code :

timer = new CountDownTimer(21000, 1) {

            public void onTick(long millisUntilFinished) {
                index++;
                timerProgressBar.setProgress((int)millisUntilFinished/1000);
            }

            public void onFinish() {
                index++;
                timerProgressBar.setProgress(0);
                navigate();
            }

        };

Upvotes: 2

Views: 1925

Answers (2)

heyitsvajid
heyitsvajid

Reputation: 1121

You should not run a busy loop like you are doing. The app might locks up. You need to let the rest of the system continue to do its work. Also, the fastest that the human eye can track is 30 milliseconds or so. The below timer counts milliseconds up to 10. You can set the maximum count up value by changing if(mcount==yourvalue).

long seconds=0;
long millis=0;
int mCount=0;
Timer timer;

//timer textview
timeflg = (TextView)findViewById(R.id.timerflg);

timer = new Timer();
       timer.scheduleAtFixedRate(new TimerTask() {
       @Override
       public void run() {
       // TODO Auto-generated method stub
                timerMethod();
       }
}, 1,100);

//method
private void timerMethod(){
   this.runOnUiThread(generate);
}
private Runnable generate= new Runnable() {
     @Override
     public void run() {
         timeflg.setText("TIME "+ seconds + ":" + mCount);
         mCount++;
         if(mCount==10){
            mCount=0;
            second++;
         }
     }
};

Upvotes: 1

heyitsvajid
heyitsvajid

Reputation: 1121

You can do it like this. Showing a 30-second countdown:

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
            index++;
            timerProgressBar.setProgress((int)millisUntilFinished/1000);
     }

     public void onFinish() {
          index++;
          timerProgressBar.setProgress(0);
          navigate();
     }
  }.start();

Upvotes: 3

Related Questions