Search code examples
javaandroidkeyboard

Disable comma or dot based on device locale in android


How can I disable dot(.) key or comma(,) key in android based on the locale language selected on the device. I need to use dot for English and comma for Swedish or other languages that support comma instead of dot.


Solution

  • You can use DigitsKeyListener from Android.

    For Ex,

    If English,

    editText.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyz1234567890."));
    

    If swedish,

    editText.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyz1234567890,"));
    

    Note that this EditText will only accepts the characters you specified in this String. So whatever chars you want you have to add it to here.