In my android application I am using a decimal
input filter to block the number of digits before decimal seperator(.)
to 14 and after dot to 4, also using a NumberFormat
with US locale to format the amount entered by user.I have noticed 2 problem when i try this as below.
decimal
when there is less than 14 Integer
digits.When change an amount greater than 999 then the first digit entered by user is not registering, I noticed that any number greater than 999 will formatted with a comma so when changing those amount with new amount the first digit typed will not come, why so.Suppose the Edittext
contain the amount 4,000.00 and then changed to 9500 then only 500 is displayed.but when we change an amount like 450.00 there is no comma then no problem,all digit will come.Below is my code.
NumberFormat formatUsTwo = NumberFormat.getInstance(Locale.US);
edtAmount=(EditText) v.findViewById(R.id.edtAmount);
edtAmount.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(14,4)});
edtAmount.setText(formatUsTwo.format(9000));
edtAmount.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus){
Double change=Double.parseDouble(edtAmount.getText().toString().trim().replace(",", ""));
edtAmount.setText(formatUsTwo.format(change));
}else{
onFocus(view, holder);
}
}
});
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;
}
}
Finally I solved the problem after spending half of the day.I changed my DecimalInputDegitsFilter class ass below which solved both the problem.
public class DecimalDigitsInputFilter implements InputFilter {
Pattern mPattern;
int digitsBeforeZero,digitsAfterZero;
public DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero) {
this.digitsBeforeZero = digitsBeforeZero;
this.digitsAfterZero = 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.toString().replace(",", ""));
if (!matcher.matches()) {
if (dest.toString().contains(".")) {
if (dest.toString().substring(dest.toString().indexOf(".")).length() > digitsAfterZero) {
return "";
}
return null;
} else if (!Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}").matcher(dest).matches()) {
if (!dest.toString().contains(".")) {
if (source.toString().equalsIgnoreCase(".")) {
return null;
}
}
return "";
}
return "";
}
return null;
}
}