I've been trying to adapt my TextInputEditText
which shows a numeric keyboard currently (it has android:inputType="numberDecimal"
in the xml) to allow input of a /
character (by clicking a button in the ui that appends a /
to the EditText
).
What I want to achieve: A numeric keyboard & allowed to enter /
in the EditText
Everything I have tried either doesn't show a numeric keyboard or doesn't allow me to add the /
to the EditText
.
Things I've tried:
android:digits="0123456789./"
to the xmlResult: It's still a numeric input and won't let me .append("/")
android:inputType="text"
in the xml and the below in the activity's onCreate()
input.setFilters(new InputFilter[] {(src, start, end, dst, dstart, dend) -> {
if(src.toString().matches("[0123456789./]+")){
return src;
}
return "";
}});
Result: I don't get a numeric keyboard (although I can only enter the characters I need which is good)
android:inputType="text"
with android:digits="0123456789./"
as this seems to be suggested quite a few times on various SO answers (and I assume must have worked at some point).Result: a non-numeric keyboard (i.e. regular text input keyboard)
Removing android:inputType
and android:digits
from the xml and also adding the below to the activity's onCreate()
seems to do the trick.
input.setKeyListener(DigitsKeyListener.getInstance("0123456789./"));
However it's not completely clear to me that this should or will always work - see note below from the docs on DigitsKeyListener
...
As for all implementations of KeyListener, this class is only concerned with hardware keyboards. Software input methods have no obligation to trigger the methods in this class.
Source: https://developer.android.com/reference/android/text/method/DigitsKeyListener