13

When creating an EditText in the java portion of the application, how do you limit it to numbers like you would in the xml? For example:

new EditText(this);

set like

<EditText
    android:id="@+id/h1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"/>
Biggsy
  • 451
  • 4
  • 9
  • 20

5 Answers5

14

Something like this perhaps ?

EditText text = new EditText(this);
text.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
tacone
  • 11,371
  • 8
  • 43
  • 60
11

Use of android:numeric is deprecated, use android:inputType="number"

akelec
  • 3,797
  • 3
  • 41
  • 39
DevZer0
  • 13,433
  • 7
  • 27
  • 51
2

Go to the docs and look at the table with XML attributes. It shows you the code equivalent of each item.

In this case, it's setRawInputType.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • setInputType might be the better choice in this case, given that it will update the KeyListener – Matthew Mar 05 '11 at 02:20
2

this will usually do it:

android:numeric="integer"

brettdj
  • 54,857
  • 16
  • 114
  • 177
NJGUY
  • 2,045
  • 3
  • 23
  • 43
1

Simply use the below lines in the xml file

To accept only Numbers put:

 android:inputType="number"

To limit the length of the input numbers:

android:maxLength="10"

To accept only specific numbers:

EX:1 The below line accept only 1 or 0.

android:digits="10"

EX:2 The below line accept only 2 or 3.

android:digits="23"
Mahmoud Zaher
  • 559
  • 6
  • 6