Skip to content

Instantly share code, notes, and snippets.

@gSrikar
Last active June 23, 2017 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gSrikar/6b5ffd6e2b8c92a1b374daec4631a134 to your computer and use it in GitHub Desktop.
Save gSrikar/6b5ffd6e2b8c92a1b374daec4631a134 to your computer and use it in GitHub Desktop.
The gist updates the color of the progress bar every few seconds like Inbox by Gmail app. The detailed explanation can be found in my blog post http://gsrikar.blogspot.com/2017/01/how-to-update-circular-progressbar.html
/**
* The amount of time that has to pass to update the progress bar color
*/
private static final long UPDATE_TIME_IN_MILLIS = 2000;
/**
* List of all the desired colors of the progress bar
*/
private ColorStateList[] colorsList;
/**
* Variable is used to choose a color from the colorsList array
*/
private int position = 0;
// UI element
private ProgressBar progressBar;
// Handler
private Handler updateProgressColorHandler;
// Runnable
private Runnable updateProgressColorRunnable;
/**
* Update the color of the progress bar every few seconds
*/
private void showAndUpdateProgressColor() {
// Initialize the color list
colorsList = new ColorStateList[]{
ColorStateList.valueOf(Color.BLUE), ColorStateList.valueOf(Color.RED),
ColorStateList.valueOf(ContextCompat.getColor(this, R.color.color_yellow)),
ColorStateList.valueOf(Color.GREEN),
ColorStateList.valueOf(Color.MAGENTA), ColorStateList.valueOf(Color.CYAN)};
// Initialize Runnable
updateProgressColorRunnable = new Runnable() {
@Override
public void run() {
Log.i(TAG, "Position: " + position);
Log.i(TAG, "Colors List Length: " + colorsList.length);
if (position >= colorsList.length) {
// There are no more elements in the array
// Reset the position
position = 0;
Log.i(TAG, "Position is set to zero");
}
// Change the progress bar color
progressBar.setIndeterminateTintList(colorsList[position]);
// Increment the position
position++;
Log.i(TAG, "Updated Position: " + position);
// Call the runnable again with a delay
updateProgressColorHandler.postDelayed(updateProgressColorRunnable, UPDATE_TIME_IN_MILLIS);
}
};
// Initialize Handler
updateProgressColorHandler = new Handler();
// Use the handler to add the runnable to the message queue
updateProgressColorHandler.post(updateProgressColorRunnable);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment