alvinjorrel
alvinjorrel

Reputation: 57

Android SeekBar value not properly being passed to calculate method

in the OnProgressChanged method of the OnSeekBarChangeListener class, I have a method that calculates the tip percentage and displays it on the screen. It only seems to work from values 0 and 100, not any value in between.

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // TODO Auto-generated method stub
                progress = ((int)Math.round(progress/5 ))*5;
                seekBar.setProgress(progress);
                String tipValStr = String.valueOf(progress);
                tipVal = progress;
                tvTipVal.setText(tipValStr);
                //Toast.makeText(TipCalc.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
                calculate(progress);
            }

public void calculate(int tip){
        try{
        DecimalFormat df = new DecimalFormat("#.00");
        billVal = Double.parseDouble(etBillVal.getText().toString());
        //tipVal = sbTipVal.getProgress();
        tipValue = billVal * (tip/100);
        billTotal = billVal + tipValue;
        tvBillVal.setText("$"+df.format(billVal));
        tvTipValue.setText("+ $" + df.format(tipValue));
        tvBillTotal.setText("$"+df.format(billTotal));
        Toast.makeText(TipCalc.this, "$"+df.format(billTotal), Toast.LENGTH_SHORT).show();
        }
        catch(Exception e){
            //billVal = 0;
        }
    }

Upvotes: 0

Views: 317

Answers (1)

ssantos
ssantos

Reputation: 16536

I'm guessing you're problem is an integer division. int tip divided by 100, will always return 0, unless tip value is 100, in which case the result is 1.

Try defining tip as a float instead.-

public void calculate(float tip);

and

tipValue = billVal * (tip / 100.0f);

Upvotes: 1

Related Questions