43

I'm making a quiz for Android and I want a limited time to answer every question. So I want to display a ProgressBar under the answers that counts down from, for example, 5 to 0 (seconds). And when it reaches zero I want to do some stuff. I have the quiz and everything working, I just want to add the ProgressBar.

Thanks in advance!

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
simtaxman
  • 613
  • 2
  • 11
  • 18

3 Answers3

61

you can use countdown timer in android .

Here is one Example you can Refer Click Here

you can use below ProgressBar in your Activity.

   <ProgressBar 
    android:id="@+id/progressbar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:max="100"
    android:progress="0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/bottom_header_relativelayout"
    />

Use CountDownTimer Like Below code in your Activity.

ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;
int i=0;

mProgressBar=(ProgressBar)findViewById(R.id.progressbar);
mProgressBar.setProgress(i);
   mCountDownTimer=new CountDownTimer(5000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
            i++;
            mProgressBar.setProgress((int)i*100/(5000/1000));

        }

        @Override
        public void onFinish() {
        //Do what you want 
            i++;
            mProgressBar.setProgress(100);
        }
    };
    mCountDownTimer.start();
Ludo
  • 743
  • 1
  • 10
  • 25
Herry
  • 7,037
  • 7
  • 50
  • 80
  • Does that work with a ProgressBar? Because that's how i want to display it. – simtaxman Apr 20 '12 at 07:05
  • Do you want to Display it on xml layout ,i mean that you don't want pop up with Progress Right. – Herry Apr 20 '12 at 07:09
  • How do I make it tick smoother? Because now it takes 5 big steps. – simtaxman Apr 21 '12 at 21:20
  • Tick smoother mean , progress show step by step increment.do you get problem in Progress update,let me known which code you have use. – Herry Apr 22 '12 at 12:05
  • Solved it, forgot to change android:max in the layout. – simtaxman Apr 23 '12 at 10:41
  • @Herry how can we count up and show progress for one second? – hasnain_ahmad May 09 '16 at 05:51
  • 2
    @hasnain_ahmad For Showing progress for 1 Sec you need to change in new CountDownTimer(1000,10) which will call by every 10 millisecond . – Herry May 09 '16 at 06:12
  • @Herry what will be the value of `millisInFuture` and `countDownInterval` to delay for 2 seconds from 0 - 2 seconds not 2 - 0, means count up. what will be value should put in the `setProgress(float progress)`? – hasnain_ahmad May 09 '16 at 10:08
  • How can I stop the countdown when the user clicks for example on a button to get to the next activity? I tried to use "GONE" but its still somehow counting – Kaiser Nov 15 '19 at 12:25
  • @Herry Could you explain this `(int)i*100/(5000/1000)`. Trying to apply your solution to a progress bar that's decreasing instead of increasing. – Jcorretjer Jan 06 '21 at 23:18
  • In case anyone needs it to work decreasing instead of increasing `progressBar.setProgress((int)(((1.0 * millisUntilFinished) / duration) * 100));` – Jcorretjer Jan 07 '21 at 00:14
47

You could use an ObjectAnimator to animate the progress of the ProgressBar:

ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", 0, 100);
animation.setDuration(5000);
animation.setInterpolator(new DecelerateInterpolator());
animation.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) { }

    @Override
    public void onAnimationEnd(Animator animator) {
        //do something when the countdown is complete
    }

    @Override
    public void onAnimationCancel(Animator animator) { }

    @Override
    public void onAnimationRepeat(Animator animator) { }
});
animation.start();
adamdport
  • 11,687
  • 14
  • 69
  • 91
2

No need for XML declaration

ProgressDialog TempDialog;
CountDownTimer CDT;
int i =5;

TempDialog = new ProgressDialog(Your_Class_Name.this);
TempDialog.setMessage("Please wait...");
TempDialog.setCancelable(false);
TempDialog.setProgress(i);
TempDialog.show();

CDT = new CountDownTimer(5000, 1000)
{
    public void onTick(long millisUntilFinished)
    {
        TempDialog.setMessage("Please wait.." + i + " sec");
        i--;
    }

    public void onFinish()
    {
        TempDialog.dismiss();
        //Your Code ...
    }
}.start();
msl
  • 109
  • 1
  • 4