Step 08 - Progress Bar

    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:

    Lets set max progress bar to 10000 in onCreate:

    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 RadioGroup paymentMethod;
    15. private ProgressBar progressBar;
    16. @Override
    17. protected void onCreate(Bundle savedInstanceState)
    18. {
    19. super.onCreate(savedInstanceState);
    20. setContentView(R.layout.activity_donate);
    21. donateButton = (Button) findViewById(R.id.donateButton);
    22. paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
    23. progressBar = (ProgressBar) findViewById(R.id.progressBar);
    24. amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
    25. amountPicker.setMinValue(0);
    26. amountPicker.setMaxValue(1000);
    27. progressBar.setMax(10000);
    28. }
    29. @Override
    30. {
    31. return true;
    32. }
    33. public void donateButtonPressed (View view)
    34. {
    35. int amount = amountPicker.getValue();
    36. int radioId = paymentMethod.getCheckedRadioButtonId();
    37. String method = radioId == R.id.PayPal ? "PayPal" : "Direct";
    38. totalDonated = totalDonated + amount;
    39. progressBar.setProgress(totalDonated);
    40. Log.v("Donate", "Donate Pressed! with amount " + amount + ", method: " + method);
    41. Log.v("Donate", "Current total " + totalDonated);
    42. }
    43. }

    Here is another version of exactly the same class:

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

    Visible here:

    1. private int totalDonated = 0;
    2. private RadioGroup paymentMethod;
    3. private ProgressBar progressBar;
    4. private NumberPicker amountPicker;

    and here:

    and here:

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