4

How can I validate a text view in android to handle positive integer numbers?

I don't want it to accept any character or signs etc...

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

5 Answers5

8

Have you taken a look at the EditText's inputType attribute? You can set a whole bunch of different input types that the EditText should limit the user input to.

From the sounds of it, you're probably looking for something like:

<EditText
    ....
    android:inputType="number" 
     ... />
MH.
  • 45,303
  • 10
  • 103
  • 116
3

IMO best way is use inputType in xml

<EditText
    ...
    android:inputType="phone" />

You have to remember that "+" is also part of the phone number.

Michal Z
  • 31
  • 3
3

You can use regular expressions. See: http://developer.android.com/reference/java/util/regex/Pattern.html

Your pattern could look like ^[0-9]{1,10}$ which means that the entered value can only consist of digits (minimum 1, maximum 10)

Marc
  • 257
  • 1
  • 7
2

Use the code in your Edittext XML to restrict any range of values

android:digits="0123456789"
jlordo
  • 37,490
  • 6
  • 58
  • 83
Neo
  • 1,359
  • 14
  • 34
0

You can use from XML

<EditText
    ...
    android:inputType="phone" />

or programmatically

EditText editText = (EditText) findViewById(R.id.yourId);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Nasimxon
  • 132
  • 1
  • 7