Mapi Mapi - 4 months ago 17
Android Question

CountDownTimer - ProgressBar stops at 90% (Java)

I want to fill the

ProgressBar
after a button click in 10 seconds. The problem is that the
ProgressBar
's progress stops at about 90%.

public void klick(View v){

mProgressBar=(ProgressBar)findViewById(R.id.progressbar);
mProgressBar.setProgress(i);
mProgressBar.setMax(100);
mCountDownTimer=new CountDownTimer(10000,100) {

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

}

@Override
public void onFinish() {
//Do what you want
b1.setText("Finished");
}
};
mCountDownTimer.start();
}


Why it stops? I thought that my code runs the
onTick()
every 0.1 seconds for 10 seconds, so that
i = 100
in 10 seconds.

Answer

this because you miss two click here is code I generate to fix your issue , I tried many numbers with i , but I figure I need just start with 3 as first tick when i = 4 time till finish will be 9797 (sometimes this time changed be 9860) but this is the average so we discover we miss two ticks.

Tick of Progress; i:4// 9797

Second part you need update progress at onFinish to avoid lose one tick more

here is the java code and log

Java Code

private ProgressBar mProgressBar;
private CountDownTimer mCountDownTimer;
int i = 3;
TextView mtextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mtextview = (TextView) findViewById(R.id.textview);
    klick();  // handle tick as you wish 
}

public void klick() {

    mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
    mProgressBar.setProgress(i);
    mProgressBar.setMax(100);
    mCountDownTimer = new CountDownTimer(10000, 100) {

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

        }

        @Override
        public void onFinish() {
            //Do what you want
            Log.v("Log_tag", "Last tick : " +i);
            mProgressBar.setProgress(i);
            mtextview.setText("Finished");
        }
    };
    mCountDownTimer.start();
}

Log

    08-02 06:26:09.637 Log_tag: Tick of Progress; i:4// 9797
08-02 06:26:09.738 Log_tag: Tick of Progress; i:5// 9696
08-02 06:26:09.839 Log_tag: Tick of Progress; i:6// 9596
08-02 06:26:09.939 Log_tag: Tick of Progress; i:7// 9495
08-02 06:26:10.046 Log_tag: Tick of Progress; i:8// 9388
08-02 06:26:10.162 Log_tag: Tick of Progress; i:9// 9272
08-02 06:26:10.263 Log_tag: Tick of Progress; i:10// 9171
08-02 06:26:10.364 Log_tag: Tick of Progress; i:11// 9070
08-02 06:26:10.464 Log_tag: Tick of Progress; i:12// 8970
08-02 06:26:10.564 Log_tag: Tick of Progress; i:13// 8870
08-02 06:26:10.665 Log_tag: Tick of Progress; i:14// 8769
08-02 06:26:10.765 Log_tag: Tick of Progress; i:15// 8669
08-02 06:26:10.866 Log_tag: Tick of Progress; i:16// 8568
08-02 06:26:10.967 Log_tag: Tick of Progress; i:17// 8467
08-02 06:26:11.069 Log_tag: Tick of Progress; i:18// 8365
08-02 06:26:11.170 Log_tag: Tick of Progress; i:19// 8264
08-02 06:26:11.272 Log_tag: Tick of Progress; i:20// 8163
08-02 06:26:11.371 Log_tag: Tick of Progress; i:21// 8063
08-02 06:26:11.472 Log_tag: Tick of Progress; i:22// 7963
08-02 06:26:11.574 Log_tag: Tick of Progress; i:23// 7861
08-02 06:26:11.673 Log_tag: Tick of Progress; i:24// 7761
08-02 06:26:11.774 Log_tag: Tick of Progress; i:25// 7660
08-02 06:26:11.874 Log_tag: Tick of Progress; i:26// 7560
08-02 06:26:11.975 Log_tag: Tick of Progress; i:27// 7459
08-02 06:26:12.077 Log_tag: Tick of Progress; i:28// 7357
08-02 06:26:12.178 Log_tag: Tick of Progress; i:29// 7256
08-02 06:26:12.294 Log_tag: Tick of Progress; i:30// 7141
08-02 06:26:12.394 Log_tag: Tick of Progress; i:31// 7040
08-02 06:26:12.496 Log_tag: Tick of Progress; i:32// 6938
08-02 06:26:12.597 Log_tag: Tick of Progress; i:33// 6837
08-02 06:26:12.699 Log_tag: Tick of Progress; i:34// 6736
08-02 06:26:12.799 Log_tag: Tick of Progress; i:35// 6636
08-02 06:26:12.899 Log_tag: Tick of Progress; i:36// 6535
08-02 06:26:13.000 Log_tag: Tick of Progress; i:37// 6434
08-02 06:26:13.101 Log_tag: Tick of Progress; i:38// 6334
08-02 06:26:13.201 Log_tag: Tick of Progress; i:39// 6233
08-02 06:26:13.302 Log_tag: Tick of Progress; i:40// 6132
08-02 06:26:13.403 Log_tag: Tick of Progress; i:41// 6031
08-02 06:26:13.505 Log_tag: Tick of Progress; i:42// 5929
08-02 06:26:13.605 Log_tag: Tick of Progress; i:43// 5829
08-02 06:26:13.706 Log_tag: Tick of Progress; i:44// 5728
08-02 06:26:13.811 Log_tag: Tick of Progress; i:45// 5624
08-02 06:26:13.912 Log_tag: Tick of Progress; i:46// 5522
08-02 06:26:14.012 Log_tag: Tick of Progress; i:47// 5422
08-02 06:26:14.112 Log_tag: Tick of Progress; i:48// 5322
08-02 06:26:14.213 Log_tag: Tick of Progress; i:49// 5221
08-02 06:26:14.315 Log_tag: Tick of Progress; i:50// 5120
08-02 06:26:14.415 Log_tag: Tick of Progress; i:51// 5019
08-02 06:26:14.518 Log_tag: Tick of Progress; i:52// 4916
08-02 06:26:14.619 Log_tag: Tick of Progress; i:53// 4815
08-02 06:26:14.721 Log_tag: Tick of Progress; i:54// 4713
08-02 06:26:14.823 Log_tag: Tick of Progress; i:55// 4611
08-02 06:26:14.924 Log_tag: Tick of Progress; i:56// 4511
08-02 06:26:15.023 Log_tag: Tick of Progress; i:57// 4411
08-02 06:26:15.124 Log_tag: Tick of Progress; i:58// 4311
08-02 06:26:15.227 Log_tag: Tick of Progress; i:59// 4207
08-02 06:26:15.327 Log_tag: Tick of Progress; i:60// 4107
08-02 06:26:15.434 Log_tag: Tick of Progress; i:61// 4001
08-02 06:26:15.541 Log_tag: Tick of Progress; i:62// 3900
08-02 06:26:15.634 Log_tag: Tick of Progress; i:63// 3800
08-02 06:26:15.735 Log_tag: Tick of Progress; i:64// 3699
08-02 06:26:15.835 Log_tag: Tick of Progress; i:65// 3599
08-02 06:26:15.935 Log_tag: Tick of Progress; i:66// 3499
08-02 06:26:16.037 Log_tag: Tick of Progress; i:67// 3398
08-02 06:26:16.137 Log_tag: Tick of Progress; i:68// 3297
08-02 06:26:16.238 Log_tag: Tick of Progress; i:69// 3196
08-02 06:26:16.340 Log_tag: Tick of Progress; i:70// 3095
08-02 06:26:16.439 Log_tag: Tick of Progress; i:71// 2995
08-02 06:26:16.540 Log_tag: Tick of Progress; i:72// 2894
08-02 06:26:16.641 Log_tag: Tick of Progress; i:73// 2793
08-02 06:26:16.741 Log_tag: Tick of Progress; i:74// 2693
08-02 06:26:16.843 Log_tag: Tick of Progress; i:75// 2591
08-02 06:26:16.943 Log_tag: Tick of Progress; i:76// 2491
08-02 06:26:17.043 Log_tag: Tick of Progress; i:77// 2391
08-02 06:26:17.143 Log_tag: Tick of Progress; i:78// 2291
08-02 06:26:17.244 Log_tag: Tick of Progress; i:79// 2190
08-02 06:26:17.345 Log_tag: Tick of Progress; i:80// 2090
08-02 06:26:17.444 Log_tag: Tick of Progress; i:81// 1990
08-02 06:26:17.545 Log_tag: Tick of Progress; i:82// 1889
08-02 06:26:17.648 Log_tag: Tick of Progress; i:83// 1786
08-02 06:26:17.749 Log_tag: Tick of Progress; i:84// 1685
08-02 06:26:17.850 Log_tag: Tick of Progress; i:85// 1585
08-02 06:26:17.951 Log_tag: Tick of Progress; i:86// 1483
08-02 06:26:18.056 Log_tag: Tick of Progress; i:87// 1378
08-02 06:26:18.157 Log_tag: Tick of Progress; i:88// 1277
08-02 06:26:18.258 Log_tag: Tick of Progress; i:89// 1177
08-02 06:26:18.358 Log_tag: Tick of Progress; i:90// 1076
08-02 06:26:18.461 Log_tag: Tick of Progress; i:91// 973
08-02 06:26:18.562 Log_tag: Tick of Progress; i:92// 872
08-02 06:26:18.662 Log_tag: Tick of Progress; i:93// 772
08-02 06:26:18.764 Log_tag: Tick of Progress; i:94// 670
08-02 06:26:18.865 Log_tag: Tick of Progress; i:95// 569
08-02 06:26:18.965 Log_tag: Tick of Progress; i:96// 469
08-02 06:26:19.066 Log_tag: Tick of Progress; i:97// 368
08-02 06:26:19.167 Log_tag: Tick of Progress; i:98// 267
08-02 06:26:19.270 Log_tag: Tick of Progress; i:99// 164
08-02 06:26:19.435 Log_tag: Last tick : 100
Comments