Wednesday, 10 February 2016

Setting a Custom Progress Bar using Linear Interpolator

Here we will be creating a custom progress bar with linear animation of progress.


We need to create a progress bar in activity_main.xml layout file with a progressDrawable which sets the background color and progress color of ProgressBar widget.

In activity_main.xml

        android:progressDrawable ="@drawable/progress_bar_retry_style" />

In progress_bar_retry_style.xml
<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="">

    <item android:id="@android:id/background">
            <solid android:color="@color/light_grey"/>

    <item android:id="@android:id/progress">
                <solid android:color="@color/light_red"/>

Now, we have to set maximum value of progress:

Suppose, if you set max to 100 and your phone has 1400 pixels between 1 and 100 point.
Then when you set progress value 1 to 2, it will cover 14 pixels which will not be shown smooth to user.

So, that’s why we choose max to 500, so that when you cover progress 1 to progress 2 , it will cover 2.8 pixels(1400/500), which will be smooth for users.

private void setProgress() {

  if (mRetryHandler == null) {
    mRetryHandler = new Handler();



  if (progress >= MAX_PROGRESS) {
    Toast.makeText(getActivity(), "Your timer has finished", Toast.LENGTH_SHORT).show();
  } else {
    mRetryHandler.postDelayed(mRetryRunnable, 200);

Basically above function will give animation, but it will feel very slow, because here we are updating progress value by 1,  5 times in 1 sec, i.e. by updating progress value to 5 in 1 sec.

We should not calculate intermediate progress values(by what progress values to be incremented in 1 sec). We should use LinerInterpolator to calculate intermediate values of progress to update in a sec.

Now, we will use ObjectAnimator to set LinearInterpolator to provide smooth animation by setting linearInterpolator on ObjectAnimator instance.


static  ObjectAnimator
ofInt(Object target, String propertyName, int... values)
Constructs and returns an ObjectAnimator that animates between int values.

This subclass of ValueAnimator provides support for animating properties on target objects. The constructors of this class take parameters to define the target object that will be animated as well as the name of the property that will be animated. Appropriate set/get functions are then determined internally and the animation will call these functions as necessary to animate the property.

targetThe object whose property is to be animated. This object should have a public method on it called setName(), where name is the value of the propertyName parameter.
propertyNameThe name of the property being animated.
valuesA set of values that the animation will animate between over time.

So, here is code:

public class MainActivity extends AppCompatActivity {

    private static final int UPDATE_RETRY_PROGRESS_BAR = 1001;
    private static final int MAX_PROGRESS = 500;
    private Handler mRetryHandler;
    private ObjectAnimator mRetryObjectAnimator;
    private ProgressBar mRetryProgressBar;

    protected void onCreate(Bundle savedInstanceState) {

        mRetryProgressBar = (ProgressBar) findViewById(;
        mRetryHandler = new RetryHandler();

        int timeFromServer = 60;
        Message msg = mRetryHandler.obtainMessage(UPDATE_RETRY_PROGRESS_BAR, timeFromServer, 0);


    private class RetryHandler extends Handler {

        private LinearInterpolator mInterpolator;

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_RETRY_PROGRESS_BAR:
                    int timeDuration = msg.arg1;
                    // for smooth progress bar update.
                    mRetryObjectAnimator =
                            ObjectAnimator.ofInt(mRetryProgressBar, "progress", MAX_PROGRESS);
                    mRetryObjectAnimator.setDuration(timeDuration * 1000);
                    if (mInterpolator == null) mInterpolator = new LinearInterpolator();

                    mRetryObjectAnimator.addListener(new Animator.AnimatorListener() {
                        public void onAnimationStart(Animator animation) {


                        public void onAnimationEnd(Animator animation) {
                            Toast.makeText(MainActivity.this, "Your timer has finished", Toast.LENGTH_SHORT).show();

                        public void onAnimationCancel(Animator animation) {


                        public void onAnimationRepeat(Animator animation) {





1 comment: