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?
I don't know why your answers are so complicated. If there's a bug in SDK you must override it or go around.
I have chosen the second way to solve that problem. If you format your string as
Locale.ENGLISH
and then put it to theEditText
(even as an empty string). Example:String.format(Locale.ENGLISH,"%.6f", yourFloatNumber);
Chasing that solution your result are compatible with the shown keyboard. Then float and double numbers work in typical for programming languages manner with dot instead of comma.
Martins answer won't work if you are instantiating the EditText programmatically. I went ahead and modified the included
DigitsKeyListener
class from API 14 to allow for both comma and period as decimal separator.To use this, call
setKeyListener()
on theEditText
, e.g.However, you still have to use Martin's trick in the
TextChangedListener
where you replace commas with periodsYou can use the following workaround to also include comma as a valid input:-
Through XML:
Programmatically:
In this way Android system will show the numbers' keyboard and allow the input of comma. Hope this answers the question :)
Android has a built in number formatter.
You can add this to your
EditText
to allow decimals and commas:android:inputType="numberDecimal"
andandroid:digits="0123456789.,"
Then somewhere in your code, either when user clicks save or after text is entered (use a listener).
This is a known bug in the Android SDK. The only workaround is to create your own soft keyboard. You can find an example of implementation here.
You could do the following:
And then you could use an input filter:
}