bipin bipin - 7 months ago 16
Java Question

DonutProgressBar value is fluctuating when I set progress value for second time

i have created a donutprogressBar and i am setting the progressbar value and its working fine for the first time but when i set the value for second time than the progressbar gets fluctuating between the first value and the second value , i want the progressbar to be set conatsntant to the second value

here is my code :

strMalePercentage = jsonObject.getJSONObject("male").getString("percentage_manglik_present");
txtPercentage.setText("Manglik Male"+strMalePercentage);
Progress(strMalePercentage);

public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_ManglikMatchMale){
linearMale.setVisibility(View.VISIBLE);
linearFemale.setVisibility(View.GONE);

imgMatchGender.setBackgroundResource(R.drawable.ic_male1);
txtPercentage.setText("Manglik Male");
Progress(strMalePercentage);
btnMale.setBackgroundColor(getResources().getColor(R.color.textPrimary));
btnMale.setBorderColor(getResources().getColor(R.color.sky_blue));
btnFemale.setBackgroundColor(getResources().getColor(R.color.light_pink));
btnFemale.setTextColor(R.color.textPrimary);
btnMale.setTextColor(R.color.gray_light);
}else
{ linearMale.setVisibility(View.GONE);
linearFemale.setVisibility(View.VISIBLE);

imgMatchGender.setBackgroundResource(R.drawable.ic_female1);
txtPercentage.setText("Manglik Female ");
Progress(strFemalePercentage);
btnFemale.setBackgroundColor(getResources().getColor(R.color.textPrimary));
btnFemale.setBorderColor(getResources().getColor(R.color.light_pink));
btnMale.setBackgroundColor(getResources().getColor(R.color.sky_blue));
btnMale.setTextColor(R.color.textPrimary);
btnFemale.setTextColor(R.color.gray_light);

}

}


public void Progress(final String Percentage){
Timer timer;


donutProgress = (DonutProgress) findViewById(R.id.donut_progress_male);

timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
double d = Double.parseDouble(Percentage);
int i = (int) d;
donutProgress.setProgress(0);
donutProgress.setProgress(i);


}
});
}
},1000,100);
}

Answer

Maybe something like this, so the timer reference is outside and you can cancel it if already running

private Timer timer;

public void progress(final String Percentage){

    donutProgress = (DonutProgress) findViewById(R.id.donut_progress_male);

    if (timer == null) {
        timer = new Timer();
    } else {
        timer.cancel();
    }
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    double d =  Double.parseDouble(Percentage);
                    int i = (int) d;
                    donutProgress.setProgress(0);
                    donutProgress.setProgress(i);


                }
            });
        }
    },1000,100);
}
Comments