0

I've created ProgressBar in my view, and I would like it to animate when it's value is updated. I'm runnig the code on API 23. How to animate progressBar when It's value is updated?

Here is my activity:

package com.brylkowski.cleaner.cleaner.Errands;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;

import com.brylkowski.cleaner.cleaner.R;

public class UploadErrandActivity extends AppCompatActivity {

    public static final String PROGRESS = "progress";
    UploadRequestTask task;
    private ProgressBar progressBar;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_errand);
        progressBar = (ProgressBar) findViewById(R.id.upload_progress);
        progressBar.setMax(6);
        task = new UploadRequestTask();
        handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                progressBar.incrementProgressBy(1);

            }
        };
        task.execute();
    }

    public class UploadRequestTask extends AsyncTask<Void, Void, Boolean> {

        UploadRequestTask() {
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            for (int i = 1; i <=6; i++){
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putInt(PROGRESS, i);
                msg.setData(bundle);
                handler.sendMessage(msg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return true;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
        }

        @Override
        protected void onCancelled() {
        }
    }
}

and my view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.brylkowski.cleaner.cleaner.Errands.UploadErrandActivity">

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminate="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/upload_progress"
        android:layout_gravity="bottom"
        android:longClickable="false"/>


</LinearLayout>
Hubert
  • 42
  • 6

1 Answers1

0

Check this for how to make ProgressBar animation. The only modification you need is instead of applying the animation once when view is initialized, you will need to call

ProgressBarAnimation anim = new ProgressBarAnimation(progressBar, progressBar.getProgress(), to);
anim.setDuration(1000);
progressBar.startAnimation(anim);  // play animation immediately

every time you want to update value. Here is documentation for view animation.

Community
  • 1
  • 1
solosodium
  • 723
  • 5
  • 15