Chance Bloom
Chance Bloom

Reputation: 3

How to set increment progress and have a progress bar update on a button press?

I can't seem to get my progress bar to update when the button is pressed. Here is how I have the code written.

    mSubmitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            double startingBudget = Double.parseDouble(mStartingBudgetInput.getText().toString());
            mProgressBar.setMax((int)(startingBudget * 100));

            double amountSpent = Double.parseDouble(mAmountSpentInput.getText().toString());
            mProgressBar.incrementProgressBy(((int)(amountSpent * 100)) / ((int)(startingBudget * 100)));

            Log.d("subButton", "onClick: " + startingBudget * 100);
        }
    });

Upvotes: 0

Views: 314

Answers (1)

rbd_sqrl
rbd_sqrl

Reputation: 420

When your button is clicked, you can call ProgressBar's setMax() and then setProgress() methods to show the progress.

Check if your progress bar XML is something like this

<ProgressBar
                    android:id="@+id/pb_products"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="match_parent"
                    android:layout_height="6dp"/>

And call a method like this to update your progress in the progress bar

private void setProgressBar(ProgressBar progressBar, int progress, int max) {
    progressBar.setMax(max);
    progressBar.setProgress(progress);
    progressBar.animate();
}

Upvotes: 1

Related Questions