Use android:inputType="number"
in your layout XML
This also works fine:
android:digits="0123456789"
Or you can add the following:
yourEditText.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL |
InputType.TYPE_NUMBER_FLAG_SIGNED);
this will accept numbers, float point numbers and negative numbers you can remove any of these as needed
You can use android:inputType="number"
in the XML file. You can specify other values such as numberDecimal as well.
Also, you might additionally want to use android:singleLine="true"
for a single line Edittext
.
Also, look into android:numeric
and android:maxLength
. maxLength in particular can be useful for setting length limitations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With