Step 08 - Progress Bar

The progress bar documentation:

  • <http://developer.android.com/reference/android/widget/ProgressBar.html>

offers us advice on using the progress bar in multi-threaded application. Not quite what we are ready for yet! (but file it away for future reference).

These two methods are probably what we need:

  • <http://developer.android.com/reference/android/widget/ProgressBar.html#setMax(int)>
  • <http://developer.android.com/reference/android/widget/ProgressBar.html#setProgress(int)>

First we would need to equip our activity with the ability to remember the donation amounts:

1. private int totalDonated = 0;

Lets set max progress bar to 10000 in onCreate:

1. progressBar.setMax(10000);

.. and set the progress in donateButtonPressed:

1. totalDonated = totalDonated + amount;
2. progressBar.setProgress(totalDonated);

Try this now and observe the progres bar.

This is the complete class:

1. package com.example.donation;

3. import android.os.Bundle;
4. import android.app.Activity;
5. import android.util.Log;
6. import android.view.Menu;
7. import android.view.View;
8. import android.widget.Button;
9. import android.widget.RadioGroup;
10. import android.widget.NumberPicker;
11. import android.widget.ProgressBar;

14. public class Donate extends Activity
15. {
16. private int          totalDonated = 0;

18. private Button       donateButton;
19. private RadioGroup   paymentMethod;
20. private ProgressBar  progressBar;
21. private NumberPicker amountPicker;

23. @Override
24. protected void onCreate(Bundle savedInstanceState)
25. {
26. super.onCreate(savedInstanceState);
27. setContentView(R.layout.activity_donate);

29. donateButton  = (Button)       findViewById(R.id.donateButton);
30. paymentMethod = (RadioGroup)   findViewById(R.id.paymentMethod);
31. progressBar   = (ProgressBar)  findViewById(R.id.progressBar);
32. amountPicker  = (NumberPicker) findViewById(R.id.amountPicker);

34. amountPicker.setMinValue(0);
35. amountPicker.setMaxValue(1000);
36. progressBar.setMax(10000);
37. }

39. @Override
40. public boolean onCreateOptionsMenu(Menu menu)
41. {
42. getMenuInflater().inflate(R.menu.donate, menu);
43. return true;
44. }

46. public void donateButtonPressed (View view)
47. {
48. int amount = amountPicker.getValue();
49. int radioId = paymentMethod.getCheckedRadioButtonId();
50. String method = radioId == R.id.PayPal ? "PayPal" : "Direct";
51. totalDonated = totalDonated + amount;
52. progressBar.setProgress(totalDonated);
53. Log.v("Donate", "Donate Pressed! with amount " + amount + ", method: " + method);
54. Log.v("Donate", "Current total " + totalDonated);
55. }
56. }

Here is another version of exactly the same class:

1. package com.example.donation;

3. import android.os.Bundle;
4. import android.app.Activity;
5. import android.util.Log;
6. import android.view.Menu;
7. import android.view.View;
8. import android.widget.RadioGroup;
9. import android.widget.NumberPicker;
10. import android.widget.ProgressBar;

12. public class Donate extends Activity
13. {
14. private int          totalDonated = 0;

16. private RadioGroup   paymentMethod;
17. private ProgressBar  progressBar;
18. private NumberPicker amountPicker;

20. @Override
21. protected void onCreate(Bundle savedInstanceState)
22. {
23. super.onCreate(savedInstanceState);
24. setContentView(R.layout.activity_donate);

26. paymentMethod = (RadioGroup)   findViewById(R.id.paymentMethod);
27. progressBar   = (ProgressBar)  findViewById(R.id.progressBar);
28. amountPicker  = (NumberPicker) findViewById(R.id.amountPicker);

30. amountPicker.setMinValue(0);
31. amountPicker.setMaxValue(1000);
32. progressBar.setMax(10000);
33. }

35. @Override
36. public boolean onCreateOptionsMenu(Menu menu)
37. {
38. getMenuInflater().inflate(R.menu.donate, menu);
39. return true;
40. }

42. public void donateButtonPressed (View view)
43. {
44. totalDonated  = totalDonated + amountPicker.getValue();
45. String method = paymentMethod.getCheckedRadioButtonId() == R.id.PayPal ? "PayPal" : "Direct";
46. progressBar.setProgress(totalDonated);

48. Log.v("Donate", amountPicker.getValue() + " donated by " +  method + "\nCurrent total " + totalDonated);
49. }
50. }

Examine them carefully. What are the differences? Why make these changes?

Not also the careful attention to spacing and alignment in the code. Not just correct indentation, but continual attention to structuring each method carefully, removing duplication and unnecessary code and formatting/aligning the declarations and assignment statements in a table like structure:

Visible here:

1. private int          totalDonated = 0;

3. private RadioGroup   paymentMethod;
4. private ProgressBar  progressBar;
5. private NumberPicker amountPicker;

and here:

1. paymentMethod = (RadioGroup)   findViewById(R.id.paymentMethod);
2. progressBar   = (ProgressBar)  findViewById(R.id.progressBar);
3. amountPicker  = (NumberPicker) findViewById(R.id.amountPicker);

and here:

1. totalDonated  = totalDonated + amountPicker.getValue();
2. String method = paymentMethod.getCheckedRadioButtonId() == R.id.PayPal ? "PayPal" : "Direct";

Android code can become very verbose and complex. Carefully formatting is essential if you are not to be overwhelmed.