The inputType
numberDecimal
in EditText
uses the dot '.' as decimal separator. In Europe it's common to use a comma ',' instead. Even though my locale is set as german the decimal separator is still the '.'
Is there a way to get the comma as decimal separator?
A variation on the 'digit' solutions offered here:
Taking into account the locale separator.
Following Code Currency Mask for EditText ($ 123,125.155)
Xml Layout
Code
My solution is:
In main activity:
char separator =DecimalFormatSymbols.getInstance().getDecimalSeparator(); textViewPitchDeadZone.setKeyListener(DigitsKeyListener.getInstance("0123456789" + separator));
In xml file:
android:imeOptions="flagNoFullscreen" android:inputType="numberDecimal"
and I took the double in the editText as a String.
I can confirm that the fixes proposed do not work on Samsung IMEs (at least on S6 and S9) and maybe LG. They still show a dot as decimal separator regardless of locale. Switching to Google's IME fixes this but is hardly an option for most developers.
It also has not been fixed in Oreo for these keyboards since it is a fix that Samsung and/or LG have to do and then to push even to their ancient handsets.
I have instead forked the number-keyboard project and added a mode where it behaves like an IME: fork. See the project sample for details. This has worked quite well for me and is similar to many of the "PIN entry" fake IMEs you see in banking apps.
It's more than 8 years passed and I am surprised, this issue isn't fixed yet...
I struggled with this simple issue since the most upvoted answer by @Martin lets typing multiple separators, i.e. user can type in "12,,,,,,12,1,,21,2,"
Also, the second concern is that on some devices comma is not shown on the numerical keyboard (or requires multiple pressing of a dot button)
Here is my workaround solution, which solves the mentioned problems and lets user typing '.' and ',', but in EditText he will see the only decimal separator which corresponds to current locale:
And the text watcher:
A workaround (until Google fix this bug) is to use an
EditText
withandroid:inputType="numberDecimal"
andandroid:digits="0123456789.,"
.Then add a TextChangedListener to the EditText with the following afterTextChanged: