Set EditText Digits Programmatically
Solution 1:
Try this:
<EditText
android:inputType="number"
android:digits="0123456789."
/>
From Code:
weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
But, it allows the user to include several "." See JoeyRA's answer for real numbers.
Solution 2:
Try this:
weightInput.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
weightInput.setKeyListener(DigitsKeyListener.getInstance(false,true));
public static DigitsKeyListener getInstance (boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.
This solve the problem about the many '.' in EditText
Solution 3:
Use InputType.TYPE_NUMBER_FLAG_DECIMAL
.
Also see: Input Types.
Solution 4:
if anyone still finding proper way, note that its setRawInputType()
not setInputType()
val digits = "ABCDabcd" // or any characters you want to allow
editText.keyListener = DigitsKeyListener.getInstance(digits)
editText.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME)