Search code examples
androidandroid-edittextandroid-textwatcher

Numeric EditText and TextWatcher


I have an decimal signed EditText (so it's impossible to write a comma). I put a TextWatcher on it but it only listen chars allowed to be written.

However I want to catch when the user tries to write a comma to show a toast.

How is it possible to do that?

Thank you


Solution

  • So I tried out the suggestion I made in the comments where you make use of setOnEditorActionListener() and instead of using the actionId, I used the KeyEvent event and checked it's value if it's equal to KeyCode.KEYCODE_COMMA. Unfortunately, it didn't work. Searched around the community and a lot of other site on how to do it, but the closest I can manage to do is to detect the KeyEvent.ACTION_DOWN.

    I think since the setting of your EditText is to already restrict the comma to be entered, the system itself is also ignoring it. One thing I don't get though is to why catch it if the user has no way of doing it?

    What I suggest for this is to make use of hint (if what I think of your use case is correct) to inform the user on what is supposed to be entered for that specific EditText. Or you could try to create your own softkeyboard. :D

    If you do, however, managed to find a workaround for this, do tell. I'm very much interested. :)

    Cheers! :D