0

I am building an android app where users can choose what amount they want to pay for an item. But I do not want to sell the items to cheap, so minimum accepted amount is .20.

Based on previous questions (here) I have written so far

if(editText.getText().toString().trim().isEmpty() || 
Integer.parseInt(editText.gettext().toString()) > 0.2 )
{
//Error message for example
} 

I want to display the currency so that the user know in what currency they are about to pay the amout they choose. I found this post but I do not know how to use the information in my code above.

I am new to this; sorry if this is a dumb question.

1 Answers1

0

Please add below class:-

class CustomRangeInputFilter implements InputFilter {
    private final double minValue;
    private final double maxValue;

    public CustomRangeInputFilter(double minVal, double maxVal) {
        this.minValue = minVal;
        this.maxValue = maxVal;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dStart, int dEnd) {
        try {
            // Remove the string out of destination that is to be replaced
            String newVal = dest.toString().substring(0, dStart) + dest.toString().substring(dEnd, dest.toString().length());
            newVal = newVal.substring(0, dStart) + source.toString() + newVal.substring(dStart, newVal.length());
            double input = Double.parseDouble(newVal);

            if (isInRange(minValue, maxValue, input)) {
                return null;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return "";
    }

    private boolean isInRange(double a, double b, double c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}

And add filter in EditText like below code:-

edittext.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 50f)});
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Dhaval Patel
  • 369
  • 1
  • 8