Search code examples
javaandroideclipsehandlerandroid-progressbar

How to make a progressBar update faster in Android?


I have a ProgressBar in an app which takes a full second to update, but I am pretty sure the thread it is on is set up update every 100ms. I want the ProgressBar to update much quicker, maybe at the 100ms level or something smoother.

Relevant code:

/**
 * Update timer on seekbar
 * */
public void updateProgressBar() {
    mHandler.postDelayed(mUpdateTimeTask, 100);
}   

/**
 * Background Runnable thread
 * */
private Runnable mUpdateTimeTask = new Runnable() {

       public void run() {
           long totalDuration = mPlayer.getDuration();
           long currentDuration = mPlayer.getCurrentPosition();

           // Displaying Total Duration time
           soundTotalDurationTextView.setText(""+utils.milliSecondsToTimer(totalDuration));
           // Displaying time completed playing
           soundCurrentDurationTextView.setText(""+utils.milliSecondsToTimer(currentDuration));

           // Updating progress bar
           int progress = utils.getProgressPercentage(currentDuration, totalDuration);
           soundProgressBar.setProgress(progress);

           // Running this thread after 100 milliseconds
           mHandler.postDelayed(this, 100);
       }
};

/**
 * Function to get Progress percentage
 * @param currentDuration
 * @param totalDuration
 * */
public int getProgressPercentage(long currentDuration, long totalDuration){
    Double percentage = (double) 0;

    long currentSeconds = (int) (currentDuration / 1000);
    long totalSeconds = (int) (totalDuration / 1000);

    // calculating percentage
    percentage =(((double)currentSeconds)/totalSeconds)*100;

    // return percentage
    return percentage.intValue();
}

Solution

  • I figured it out in case anyone ever needs this. I changed the divisor on currentSeconds and totalSeconds to 100, from 1000. This gave me a larger(and more precise) number.

     /**
     * Function to get Progress percentage
     * @param currentDuration
     * @param totalDuration
     * */
    public int getProgressPercentage(long currentDuration, long totalDuration){
        Double percentage = (double) 0;
    
        long currentSeconds = (int) (currentDuration / 100);
        long totalSeconds = (int) (totalDuration / 100);
    
        // calculating percentage
        percentage =(((double)currentSeconds)/totalSeconds)*100;
    
        // return percentage
        return percentage.intValue();
    }