Code Example

Android ProgresBar Snippets

Learn about ProgressBar implementation and usage in android via simple code snippets and examples.

1. Android ProgressBar and ValueAnimator Example

A simple step by step Android ProgressBar with ValueAnimator code example.

Update a ProgressBar based on the value of an animation created using ValueAnimator.

This example will comprise the following files:

  • MainActivity.java

Step 1: Create Project

  1. Open your AndroidStudio IDE.
  2. Go to File-->New-->Project to create a new project.

Step 2: Dependencies

No third party or special dependency is needed.

Step 3: Design Layouts

*(a). activity_main.xml

Create a file named activity_main.xml and design it as follows:

Inside a RelativeLayout add a Button and ProgressBar:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    tools:context="com.demo.myprogressbar.MainActivity">

    <ProgressBar
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:id="@+id/pb"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:progressDrawable="@drawable/bar_color"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/bt"
        android:text="点我"/>
</RelativeLayout>

Step 4: Write Code

Write Code as follows:

(a). MainActivity.java

Create a file named MainActivity.java

Here is the full code

package com.demo.myprogressbar;

import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb);
        progressBar.setMax(100);
        progressBar.setProgress(50);
        Button button = (Button) findViewById(R.id.bt);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ValueAnimator valueAnimator = ValueAnimator.ofInt(50,40);
                valueAnimator.setDuration(2000);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        progressBar.setProgress((Integer) valueAnimator.getAnimatedValue());
                    }
                });
                valueAnimator.start();
            }
        });

    }
}

Run

Simply copy the source code into your Android Project,Build and Run.

Read More.

Related Posts