0

I am trying to show "," instead of "." in android soft keyboard for supporting decimal numbers in Europe countries. I tried to add a decimal separator by setting a key listener programmatically. But when I tried to set the separator, it is showing ".-" instead of ".".

How can I show only the dot?

This is how I tried to show the separator.

char separator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
mWeightEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789" + separator));     
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • My fix for this problem is here: [enter link description here](https://stackoverflow.com/a/63520135/11033601) – Alex K Aug 21 '20 at 09:36

1 Answers1

0

If you want to have "," instead of the ".-" you need to set the input type of your editext to

mWeightEditText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

EDIT I forgot to add you the inputFilter

public static InputFilter[] setPriceFilters() {
    return new InputFilter[]{new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            int digitsBeforeZero = 20;
            int digitsAfterZero = 2;
            String res = dest.toString().replace(" ", "");
            if (res.isEmpty() && source.toString().equals(".")) {
                return "";
            } else if (res.contains(",") && source.toString().equals(".")) {
                return "";
            } else if (res.isEmpty() && source.toString().equals("0")){
                return "";
            }
            Matcher matcher = Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\,[0-9]{0," + (digitsAfterZero - 1) + "})?)?").matcher(res);
            if (!matcher.matches())
                return "";
            return null;
        }
    }};
}

In my version I have a digit after and before zero check, you can remove it on the matcher if you don't need it

Vodet
  • 1,491
  • 1
  • 18
  • 36
  • I am already setting the same but it's not working. mWeightEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); mWeightEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(inputLength)}); – Jayashree S Dec 16 '19 at 10:41
  • I have edited my answer, tell me if this work for you use it like this : mWeightEditText.setFilters(setPriceFilters()); – Vodet Dec 16 '19 at 10:53