Search code examples
androidprogress-barcountdowntimer

Countdown Timer with Progressbar not show progress


i made a countdown timer with progress bar but progress bar not show in percent

like this enter image description here

in countdown i use 60000 milisecond (1 minute) but it now show in progressbar with setprogress

i make xml like this

<ProgressBar
                android:id="@+id/progressbartimer"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:min="0"
                android:indeterminate="true"
                android:visibility="visible" />

in updatetimer i used timelefttxt just to see countdown work or not

    public void startstop() {
     if(timerunning){
         stoptimer();
     }else{
         startimer();
     }
    }

    public void startimer() {
        countDownTimer = new CountDownTimer(timeleftinmilisecond,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeleftinmilisecond = millisUntilFinished;
                updatetimer();
            }

            @Override
            public void onFinish() {
                Toast.makeText(pretest.this,"jawaban kamu salah",Toast.LENGTH_SHORT).show();
                startnum++;
                Dialogsalah(pembahasan);
                tampiljawaban.add(new tampiljawaban(question,jawaban,"-"));
            }
        }.start();
        timerunning = true;
    }

    public void updatetimer() {
        int progres = (int) ( timeleftinmilisecond/timefull )*100;
        progressBar.setProgress(progres);

        int minute = (int) timeleftinmilisecond/60000;
        int second = (int) timeleftinmilisecond % 60000 / 1000;

        String timelefttxt;
        timelefttxt = "" + minute;
        timelefttxt += ":";
        if(second < 10) timelefttxt += "0";
        timelefttxt += second;

        timertv.setText(timelefttxt);
    }

Solution

  • First, you need to remove the android:indeterminate="true" from XML use progress bar like this without that attribute

      <ProgressBar
                android:id="@+id/progressbartimer"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="100"
                android:min="0"
                android:visibility="visible" />
    

    And set progress in updateTimer() like this

     public void updatetimer() {
         double progress = (60000 - millisUntilFinished) / 1000; // I have hardcoded max timer value i.e 60000 milliseconds i.e 60s 
                progressBar.setProgress((int) progress);
    
        int minute = (int) timeleftinmilisecond/60000;
        int second = (int) timeleftinmilisecond % 60000 / 1000;
    
        String timelefttxt;
        timelefttxt = "" + minute;
        timelefttxt += ":";
        if(second < 10) timelefttxt += "0";
        timelefttxt += second;
    
        timertv.setText(timelefttxt);
    }
    

    Note : You might need to set the android:max attribute to 60 if you are using this code