12

I set InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_CLASS_NUMBER to my EditText and i want to use comma for decimal separator. So i set digits "0123456789.," to EditText.

editText.keyListener = DigitsKeyListener.getInstance("0123456789.,")

I set a TextWatcher on EditText to handle user input. When i clicked comma(",") on Android emulator keyboard, it is working as expected but if i get build on my phone, which has Samsung Keyboard, comma key is disabled and doesnt work. I searched so much but i couldn't find a way.

Any idea with this problem?

enter image description here

Darkhmar
  • 137
  • 1
  • 7
  • Out of interest what happens if you remove the period from your list and only have the numbers and the comma? – David Kroukamp Dec 07 '20 at 16:12
  • 1
    Nothing changes, unfortunately. I've tried too many things. – Darkhmar Dec 07 '20 at 16:34
  • Yeah I think it would depend on your phones current input/region settings. I mean you can't choose what decimal separator you want, in the UK for example the decimal separator is a period, in South Africa its comma. You wouldn't get to choose which one to use. So maybe try setting your phones input or region to a country whos decimal separator is a comma and see if that works – David Kroukamp Dec 07 '20 at 16:36
  • Have you also seen this post? In case the above doesn't work https://stackoverflow.com/a/6280607/1133011 – David Kroukamp Dec 07 '20 at 16:38
  • I am running it by applying the region related change to the edittext anyway sir. I also saw the answer in the link you gave, I even read a lot of posts related to this subject, but thank you for your interest. The situation here may be phone-based as you said, but I still want to solve this situation. – Darkhmar Dec 07 '20 at 17:10
  • @Darkhmar: Did u find any solution? I am facing the same issue. – UrMi Feb 05 '21 at 13:32
  • 1
    Unfortunately i couldn't find any solution to fix this. I just gave same action to dot and comma. But i will work on this issue later for fix, if i can fix this issue, i will update the post. (or you fix the issue, you can share the solution with us of course :D) – Darkhmar Feb 06 '21 at 22:29
  • 3
    News? I am facing the same issue too. – Innova Jul 30 '21 at 11:26

2 Answers2

0

It's the year 2022, and still this problem exists (at least on Samsung devices):)

So I solved this by adding a TextChangedListener to the EditText and check, whether the last entered character is equal to a (country specific) thousands separator. If so, I replace it by a country specific decimal separator:

 public class EditTextTausenderErsetzer implements TextWatcher{
 ...
    @Override
    public void afterTextChanged(Editable editable) 
    {
      // determine country specific separators
      // char thousandsSep = .., char commaSep =..

      int comma_index = editable.toString().indexOf(commaSep);
        if(comma_index == -1) {  // no comma so far, thus there might be a "." which shall be a comma separator
         if (editable.toString().indexOf(thousandsSep) != -1) {
            if (editable.toString().indexOf(thousandsSep, (editable.length()-1)) == (editable.length() - 1)) {
                    editable.delete(editable.length() - 1, editable.length() - 1);
                    editable.insert(editable.length(), commaSep + "");

                }
            }
        }
        // ...
        // do other stuff like setting automatically thousands separators

        

This solution works, if your app manages the thousands separators, and you do not allow the user to set these.

tayfun
  • 1
  • 1
0

I solved in this way

override fun afterTextChanged(editable: Editable) {
// If the user input from keyboard some special characters like dot, we replace it programmatically with comma
if (editable.toString().contains(".")) {
    editable.delete(editable.toString().length - 1, editable.toString().length)
    editable.insert(editable.toString().length, ",")
    return
} }
Marco
  • 1
  • 1