18

How to set only Integer numbers for EditText ? (I would like to avoid to user to put comma)

wawanopoulos
  • 9,614
  • 31
  • 111
  • 166
  • I really feel like it was asked before but I cannot find an exact match. There is [http://stackoverflow.com/questions/4645119/how-to-set-only-numeric-value-for-edittext-in-android](http://stackoverflow.com/questions/4645119/how-to-set-only-numeric-value-for-edittext-in-android) and [How to force EditText to accept only numbers?](http://stackoverflow.com/questions/9334314/how-to-force-edittext-to-accept-only-numbers) – Artjom B. Jun 11 '14 at 10:26
  • I think you should reconsider your accepted answer. – Artjom B. Jun 11 '14 at 10:35

7 Answers7

29

Try below attribute in your xml:

android:inputType="numberSigned"
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • Thanks, And programmatically ? – wawanopoulos Jun 11 '14 at 10:21
  • 3
    customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); try this – Pratik Dasa Jun 11 '14 at 10:24
  • 1
    Do **not** set `InputType.TYPE_NUMBER_FLAG_DECIMAL` as this will allow for decimals. And you may want to use setInputType() instead of setRawInputType() [see here](https://stackoverflow.com/questions/9933574/edittext-setinputtype-vs-setrawinputtype) – Erik Bongers Apr 11 '21 at 11:33
13

You can set following in xml:

android:inputType="numberSigned"

Programmatically:

editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Niko
  • 8,093
  • 5
  • 49
  • 85
3

Programmatically you can achieve this like :

edittext.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER);
Nikunj
  • 3,937
  • 19
  • 33
NSetty
  • 140
  • 11
2

Use android:inputType="numberSigned" in xml: "EditText " Or

In Activity:

                   EditText et=(EditText)findViewById(R.id.et_edit);
                   et.setInputType(InputType.TYPE_CLASS_NUMBER);
Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
1
   <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:ems="10"
        android:inputType="numberSigned" >>
    </EditText>
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
0

above answers are correct. you can do it dynamically also from java -

EditText ed=(EditText)findViewById(R.id.edit);
ed.setInputType(InputType.TYPE_CLASS_NUMBER);
Ronn Wilder
  • 1,228
  • 9
  • 13
-1

Use this code inside XML

android:inputType="number"

Below Other Options are:

In XML just add below line,

android:digits="1234567890" ---> It will allow only numbers.

Mani kandan
  • 359
  • 4
  • 12