I’m working on a debt tracking app, and have need for a progress bar that as the current debt goes down, the progress goes up. The only way I can think to do this would be to fudge the numbers around so that the progress bar MAX is set to 0, and then change the starting debt to be a negitive number, as follows
ProgressBar.setMax(0);
Integer startingdebt = -1000; // 1000$ owed
ProgressBar.setProgress(currentlyowed); //say, -500
so you start with -1000, and add 500$ to it if you paid it. I don’t know if this would work though, because I’m sure the progressbar control has a set minimum of 0 already, so you can’t set max the same…is there a way to do this?
Edit: Thanks for the answers guys, but I found an easier way:
to share with others, here’s my final code block:
double startingamount = (this.c.getDouble(this.c.getColumnIndex(DbAdapter.KEY_STARTINGAMOUNT)));
double currentamount = (this.c.getDouble(this.c.getColumnIndex(DbAdapter.KEY_CURRENTAMOUNT)));
currentdebt.setText(formatter.format(currentamount));
double progresspaid = new Double(startingamount-currentamount);
double progresspercentage = new Double((progresspaid / startingamount)*100);
int progresspercent = (int)progresspercentage;
progressbar.setText(Integer.toString(progresspercent)+"%");
progressbar.setMax(100);
progressbar.setProgress(progresspercent);
the key was to get a variable that subtracts the current amount from the starting amount, and then divides that by the starting amount.
double progresspaid = new Double(startingamount-currentamount);
double progresspercentage = new Double((progresspaid / startingamount)*100);
Thanks again though, I really appreciate people helping me learn Java and android development, I’m a VB.net developer so some of this is still foreign to me.
While this may not be ideal (I don’t use Android and avoid UI coding ;-), the numbers and percentages can be calculated with a little math — it may be problematic if the raw value is itself displayed on the progress bar.
The percent going from 0 … 100 is:
(When min = 0, this is trivially
percent = current / max.)And the percent going from 100 to 0 is:
or, expanded:
Example, current = -800, max = 0, min = -1000:
Another example, current = 0, max = 2000, min = -1000:
Happy coding.