1

I'm trying to create a filter for my edittext to have a max number with max decimal after point. I want to use this editText for money typing.

I set the inputType to numberDecimal but I can write infinite number with lot of digits after decimal point.

I found lot of thread on StackOverFlow to filter the max decimal, but I want to add a max number too.

So, I just want to have filter to write between [0 and 1000] with 2 digits after decimal point, but 1000 is the max. (can't write 1000.99).

Thanks

Naografix
  • 755
  • 1
  • 11
  • 32
  • https://stackoverflow.com/questions/27077507/android-how-to-only-allow-a-certain-number-of-decimal-places this might be helpful – vinay Maneti Aug 30 '17 at 08:07

1 Answers1

3

You can achieve that using InputFilters

first you need to create an input filter for the decimal digits

public class DecimalDigitsInputFilter implements InputFilter {

Pattern mPattern;

public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero) {
    mPattern=Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?");
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

    Matcher matcher=mPattern.matcher(dest);       
    if(!matcher.matches())
        return "";
    return null;
}

}

Then create another input filter to limit numbers between 0 and 1000

public class InputFilterMinMax implements InputFilter {

private int min, max;

public InputFilterMinMax(int min, int max) {
    this.min = min;
    this.max = max;
}

public InputFilterMinMax(String min, String max) {
    this.min = Integer.parseInt(min);
    this.max = Integer.parseInt(max);
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {   
    try {
        int input = Integer.parseInt(dest.toString() + source.toString());
        if (isInRange(min, max, input))
            return null;
    } catch (NumberFormatException nfe) { }     
    return "";
}

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

then set these input filters to your edit text

 edittext.setFilters(new InputFilter[]{ new InputFilterMinMax("0", "1000"), new DecimalDigitsInputFilter(3,2)});

I haven't tried the code but I think this will put you on the right way.

References:

Limit Decimal Places in Android EditText

Is there a way to define a min and max value for EditText in Android?

Muhammad Ashraf
  • 1,232
  • 1
  • 14
  • 31