1

I'm using Progress Bars to show information in my app, which are initially drawn fine using code like this:

ProgressBar exampleProgressBar = (ProgressBar) findViewById(R.id.progressBarExampleID);
    exampleProgressBar.setMax(exampleMaxValue);
    exampleProgressBar.setProgress(exampleProgressValue);

I then want the user to be able to input information, click a button to save, and then the application to use that information to edit the text above it and set the Progress Bar to a new position.

When the user clicks the button, the same method as before is run but with new variables. The strange thing is that sometimes it updates the Progress Bar but most of the time it just stays in the same position. The text changes every time no problem.

Does anyone have any ideas as to what might be causing this?

Matt Harris
  • 3,496
  • 5
  • 32
  • 43

2 Answers2

4

There is a slight bug when changing Progress Bars. After they have been initially set, if you want to then change the values you need to set it to zero in between:

ProgressBar dataProgressBar = (ProgressBar) findViewById(R.id.progressBarData);

    //set to 0 first to stop the bug from happening

    dataProgressBar.setMax(0);
    dataProgressBar.setProgress(0);

    //set your new values

    dataProgressBar.setMax((int) dataAllowanceValue);
    dataProgressBar.setProgress((int) dataMBytes);
Matt Harris
  • 3,496
  • 5
  • 32
  • 43
0
      ProgressBar exampleProgressBar =null;
     int exampleMaxValue=100;
     int i = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
    exampleProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
    exampleProgressBar.setMax(exampleMaxValue);
    exampleProgressBar.setProgress(0);

}
    @Override
    public void onClick(View v) {
        i++;
        exampleProgressBar.setProgress(i);
    }

Declare the ProgressBar object as field, apply only once findViewById(). Make sure your not doing something in background thread and trying to update the progress bar from that thread (if so, check Painless Threading in Android Documentation) or you are doing some badass operation and set the progress in same time, so the UI-thread is unable to respond. Hope it works.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Thanks for replying, I did a little research and found that there is a slight bug sometimes when setting a progress bar and then setting it again to something different. The solution to the problem is to set it's values to zero in between the change. – Matt Harris Dec 27 '11 at 12:40