How to calculate ProgressBar percentage for daily drink water Code Answer

I made an app about android water drinking monitor. But it does the wrong calculation as a percentage. What do you think I might have done wrong. (When the progress bar is 100 at 100, the daily water calculation is wrong)

waterml = sharedPreferences.getInt(WATER_ML, 0);
waterdata = sharedPreferences.getInt(WATER_DATA, 0);
int dailywater = 2838; // example        
ml100.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       //scale 100 ml
        int percent = 100  * 100 / dailywater;
        ProgressBarAnimation anim = new ProgressBarAnimation(circlebar, waterdata, (waterdata + percent));
        anim.setDuration(300);
        circlebar.startAnimation(anim);
        dailywater.setText((waterml + 100) + " / " + dailywater + " ml");
        editor = sharedPreferences.edit();
        editor.putInt(WATER_DATA, (waterdata + percent));
        editor.putInt(WATER_ML, (waterml + 100));
        editor.apply();
    }
});
////
public static class ProgressBarAnimation extends Animation {
    private CircleProgressBar progressBar;
    private float from;
    private float to;
    ProgressBarAnimation(CircleProgressBar progressBar, float from, float to) {
        super();
        this.progressBar = progressBar;
        this.from = from;
        this.to = to;
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        float value = from + (to - from) * interpolatedTime;
        progressBar.setProgress((int) value);
    }
}

Answer

It seems to me like your code, namely editor.apply(); is working as follows:

you are trying to save the progress and it occurs that sometimes it is called multiple times in a row. According to documentation if you are sending several apply() or commit() commands only the last one will be executed. Therefore, some clicks might not be processed.

I suggest you simply changing your variable for progress and saving it later:

waterml = sharedPreferences.getInt(WATER_ML, 0);
waterdata = sharedPreferences.getInt(WATER_DATA, 0);
int dailywater = 2838; // example        
ml100.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //scale 100 ml
        int percent = 100  * 100 / dailywater;
        ProgressBarAnimation anim = new ProgressBarAnimation(circlebar, waterdata, (waterdata + percent));
        anim.setDuration(300);
        circlebar.startAnimation(anim);
        dailywater.setText((waterml + 100) + " / " + dailywater + " ml");

        waterdata += percent;
        waterml += 100;
    }
});

and then in your onPause() function you simply save them:

public void onPause() {
    editor = sharedPreferences.edit();
    editor.putInt(WATER_DATA, waterdata);
    editor.putInt(WATER_ML, waterml);
    editor.apply();
}

If you want your percent not to be rounded, you could change putInt to putFloat and getInt to getFloat

Related Posts

© No Copyrights, All Questions are retrived from public domain.
Tutorial Guruji