Progress bar and Handler

In class on Monday 3 March, I showed off the ProgressBar widget and using a Handler to schedule events to occur over time.

To start, I added this widget to my main_fragment.xml:

    <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar"
            android:layout_gravity="center_horizontal"
            android:max="100"
            android:progress="75"
            android:indeterminate="false"/>

It uses the progress and max attributes to determine where to show the bar, so 75/100 is 75% across.

Now, I want my bar to start out invisible, reveal itself when the user clicks the button, and then hide again when they hit the back button to return to this activity. This can be done by overriding life-cycle methods like onResume. You would do this in the PlaceholderFragment class (or directly in MainActivity if you’re not using fragments).

First, in the designated class, but before any methods, add these variable declarations:

    ProgressBar progress;
    Handler handler = new Handler();
    final int progressIntervalMS = 250;

You’ll have to initialize the progress field in the onCreateView method. (If you’re not using fragments, it’s in onCreate, and you leave out the rootView. part.)

    progress = (ProgressBar)rootView.findViewById(R.id.progressBar);

Now, in the onClick method we call postDelayed so that we’ll get a callback after a specified number of milliseconds.

    progress.setVisibility(View.VISIBLE);
    progress.setProgress(0);
    handler.postDelayed(PlaceholderFragment.this, progressIntervalMS);

(Replace PlaceholderFragment.this with MainActivity.this if you’re not using fragments.) The above method call should get flagged as an error, because this class does not implement Runnable, so let’s fix that now. Add implements Runnable to the class declaration of the Fragment:

public static class PlaceholderFragment extends Fragment implements Runnable {

or the Activity:

public class MainActivity extends ActionBarActivity implements Runnable {

and then add your run() method. This was mine:

    @Override
    public void run() {
        // Increase progress setting by 10
        progress.setProgress(progress.getProgress() + 10);
        if(progress.getProgress() >= 90) {  // Once we've reached 90,
            // Transition to SecondActivity
            Intent intent = new Intent(getActivity(), SecondActivity.class);
            startActivity(intent);
        }
        else {  // Otherwise, wait longer and call us back again.
            handler.postDelayed(this, progressIntervalMS);
        }
    }

Finally, we want to ensure that the progress bar is hidden when the activity first starts, and when we return to this activity using the back button.

    @Override
    public void onResume() {
        super.onResume();
        progress.setVisibility(View.INVISIBLE);
    }