3

I am trying to create a smooth progressbar with a countdown timer. I want the timer will countdown upto 20 sec.I am using the below code but I unable to achieve the smooth decending of the progressbar. Countdown tick function is working fine with the progressbar, but not in case of smooth behaviour. I found the below code from stackoverflow which i used in my project, but can you tell me what i am doing wrong.

 if(android.os.Build.VERSION.SDK_INT >= 11){
        // will update the "progress" propriety of seekbar until it reaches progress
        ObjectAnimator animation = ObjectAnimator.ofInt(progressBarTimer, "progress", 0, 500);
        animation.setDuration(20000); // 0.5 second
        animation.setInterpolator(new LinearInterpolator());
        animation.start();
    }
    else
        progressBarTimer.setProgress(100);

enter image description here

Ramanuj Basu
  • 77
  • 3
  • 15

3 Answers3

2

I just Divide progressbar to 1000 parts and call every 100 milisecond so it ll be more smooth

public class MainActivity extends AppCompatActivity {

ProgressBar barTimer;
CountDownTimer countDownTimer;
TextView textTimer;
@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    barTimer = findViewById(R.id.barTimer);


   barTimer.setMax(1000);
    startTimer(10); 

} 

private void startTimer(final int minuti) {
    countDownTimer = new CountDownTimer(60 * minuti * 1000, 100) {
        @Override 
        public void onTick(long leftTimeInMilliseconds) {

            long seconds = leftTimeInMilliseconds / 600;

            barTimer.setProgress((int)seconds);

        } 
        @Override 
        public void onFinish() { 

        } 
    }.start();

} 
} 
Vivek Hirpara
  • 787
  • 7
  • 17
0

I achieved this by following code:

val ORDER_ACCEPT_TIME = 5000L //5 seconds
val INTERVAL = 30

progressBar.max = ORDER_ACCEPT_TIMER_TIME

private fun startTimer(){
        pauseTimer = object : CountDownTimer(ORDER_ACCEPT_TIMER_TIME, INTERVAL){
            override fun onTick(millisUntilFinished: Long) {

                val secondsInMilli: Long = 1000

                val diff = millisUntilFinished
                val elapsedSeconds = diff / secondsInMilli

                val progress = INTERVAL + progressBar.progress
                progressBar.progress = progress.toInt()

                tvProgress.text = elapsedSeconds.toString() //(optional) to show countdown textview
                
            }

            override fun onFinish() {
                
            }
        }

        pauseTimer.start()
    }
Asad
  • 1,241
  • 3
  • 19
  • 32
0

Kotlin Code

import android.animation.ObjectAnimator;
import android.view.animation.DecelerateInterpolator;

public void setProgressWithAnimation(float progress) {
    ObjectAnimator anim = ObjectAnimator.ofFloat(this, "show progress", progress);
    anim.setDuration(6000);
    anim.setInterpolator(new DecelerateInterpolator());
    anim.start();
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Amit
  • 1
  • 2
  • @suraj-rao Is ObjectAnimator better or CountDownTimer? I am asking this becuase my use case is I have list...so each item will have own progress bar. – Shadow Droid Apr 06 '23 at 08:36