How to make EditText accept only numbers?

How to make EditText accept only numbers.?

+43
android android-edittext
Feb 17 '12 at 19:39
source share
4 answers

Use android:inputType="number" in your XML layout

+80
Feb 17 2018-12-17T00:
source share

This also works great:

 android:digits="0123456789" 
+34
Nov 06 '12 at 17:08
source share

Or you can add the following:

 yourEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED); 

this will take numbers, floating point numbers and negative numbers you can remove any of them if necessary

+26
Feb 17 2018-12-17T00:
source share

You can use android:inputType="number" in the XML file. You can also specify other values, such as numberDecimal.

Alternatively, you can optionally use android:singleLine="true" for a single line of Edittext .

Also check out android:numeric and android:maxLength . maxLength in particular can be useful for setting length limits.

+13
Feb 17 '12 at 19:49
source share



All Articles