ProgressBar and delayed call via Handler in Android

This is simple note how to use SmoothProgressBar and asynchronous increment it via Hanlder.

By default, application code runs in the main thread. Every statement is therefore executed in sequence. If you perform a long lasting operation, the application blocks until the corresponding operation has finished. To provide a good user experience all potentially slow running operations in an Android application should run asynchronously.

Android supports the usage of the Thread class to perform asynchronous processing. You can also use the android.os.Handler class or the AsyncTasks classes.

A Handler object registers itself with the thread in which it is created. It provides a channel to send data to this thread. For example, if you create a new Handler instance in the onCreate() method of your activity, it can be used to post data to the main thread. The data which can be posted via the Handler class can be an instance of the Message or the Runnable class. A Handler is particular useful if you have want to post multiple times data to the main thread.

First, add dependencies to build.gradle (Module app)

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.github.castorflex.smoothprogressbar:library:1.1.0'
    compile 'com.github.castorflex.smoothprogressbar:library-circular:1.2.0'
}

Second, describe MainActivity

public class MainActivity extends AppCompatActivity {
    CircularProgressBar cb;
    SmoothProgressBar pb;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);

        cb = (CircularProgressBar) findViewById(R.id.circuler);
        pb = (SmoothProgressBar) findViewById(R.id.linear);
    }

    public void startProgress(View v) {
        Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show();
        //int p = pb.getProgress();
        //pb.setProgress(p + 1);
        pb.setProgress(0);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 10; i++) {
                    final int value = i;
                    doFakeWork();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            pb.setProgress(value);
                        }
                    });
                }
            }
        };
        new Thread(runnable).start();
    }

    private void doFakeWork() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void resetProgress(View v) {
        pb.setProgress(0);
    }
}

Third, set activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        style="@style/SmoothProgressBar"
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:progress="0"
        android:indeterminate="false"
        app:spb_sections_count="4"
        app:spb_color="#FF0000"
        app:spb_speed="2.0"
        app:spb_stroke_width="4dp"
        app:spb_stroke_separator_length="4dp"
        app:spb_reversed="false"
        app:spb_mirror_mode="false"
        app:spb_progressiveStart_activated="true"
        app:spb_progressiveStart_speed="1.5"
        app:spb_progressiveStop_speed="3.4"
        />

    <fr.castorflex.android.circularprogressbar.CircularProgressBar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/circuler"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:indeterminate="true"
        android:layout_gravity="center_horizontal"
        app:cpb_color="#FF0000"
        app:cpb_colors="@array/colors"
        app:cpb_rotation_speed="1.0"
        app:cpb_sweep_speed="1.0"
        app:cpb_stroke_width="4dp"
        app:cpb_min_sweep_angle="10"
        app:cpb_max_sweep_angle="300"
        />

    <Button android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startProgress"
        android:text="Start"
        android:layout_gravity="center_horizontal"/>

    <Button android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="resetProgress"
        android:text="Reset"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

Fourth, set array of colors

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

    <color name="holo_blue_dark">#0099cc
    <color name="holo_yellow_dark">#ff8800
    <color name="holo_green_dark">#669900
    <color name="holo_purple_dark">#9933cc
    <color name="holo_red_dark">#cc0000

    <integer-array name="colors">
        <item>@color/holo_blue_dark
        <item>@color/holo_yellow_dark
        <item>@color/holo_green_dark
        <item>@color/holo_purple_dark
        <item>@color/holo_red_dark
    </integer-array>

</resources>

Result

android_progressbar.png
comments powered by Disqus