Step 08 - Progress Bar

The progress bar documentation:

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:

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;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.util.Log;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.RadioGroup;
  9. import android.widget.NumberPicker;
  10. import android.widget.ProgressBar;
  11. public class Donate extends Activity
  12. {
  13. private int totalDonated = 0;
  14. private Button donateButton;
  15. private RadioGroup paymentMethod;
  16. private ProgressBar progressBar;
  17. private NumberPicker amountPicker;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState)
  20. {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_donate);
  23. donateButton = (Button) findViewById(R.id.donateButton);
  24. paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
  25. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  26. amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
  27. amountPicker.setMinValue(0);
  28. amountPicker.setMaxValue(1000);
  29. progressBar.setMax(10000);
  30. }
  31. @Override
  32. public boolean onCreateOptionsMenu(Menu menu)
  33. {
  34. getMenuInflater().inflate(R.menu.donate, menu);
  35. return true;
  36. }
  37. public void donateButtonPressed (View view)
  38. {
  39. int amount = amountPicker.getValue();
  40. int radioId = paymentMethod.getCheckedRadioButtonId();
  41. String method = radioId == R.id.PayPal ? "PayPal" : "Direct";
  42. totalDonated = totalDonated + amount;
  43. progressBar.setProgress(totalDonated);
  44. Log.v("Donate", "Donate Pressed! with amount " + amount + ", method: " + method);
  45. Log.v("Donate", "Current total " + totalDonated);
  46. }
  47. }

Here is another version of exactly the same class:

  1. package com.example.donation;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.util.Log;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.RadioGroup;
  8. import android.widget.NumberPicker;
  9. import android.widget.ProgressBar;
  10. public class Donate extends Activity
  11. {
  12. private int totalDonated = 0;
  13. private RadioGroup paymentMethod;
  14. private ProgressBar progressBar;
  15. private NumberPicker amountPicker;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState)
  18. {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_donate);
  21. paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
  22. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  23. amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
  24. amountPicker.setMinValue(0);
  25. amountPicker.setMaxValue(1000);
  26. progressBar.setMax(10000);
  27. }
  28. @Override
  29. public boolean onCreateOptionsMenu(Menu menu)
  30. {
  31. getMenuInflater().inflate(R.menu.donate, menu);
  32. return true;
  33. }
  34. public void donateButtonPressed (View view)
  35. {
  36. totalDonated = totalDonated + amountPicker.getValue();
  37. String method = paymentMethod.getCheckedRadioButtonId() == R.id.PayPal ? "PayPal" : "Direct";
  38. progressBar.setProgress(totalDonated);
  39. Log.v("Donate", amountPicker.getValue() + " donated by " + method + "\nCurrent total " + totalDonated);
  40. }
  41. }

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;
  2. private RadioGroup paymentMethod;
  3. private ProgressBar progressBar;
  4. 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.