How to set only Integer numbers for EditText ? (I would like to avoid to user to put comma)
Asked
Active
Viewed 2.2k times
17
wawanopoulos
- 9,016
- 29
- 105
- 158
-
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 Answers
29
Try below attribute in your xml:
android:inputType="numberSigned"
Pratik Dasa
- 7,389
- 4
- 29
- 44
-
-
3customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); try this – Pratik Dasa Jun 11 '14 at 10:24
-
1Do **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
- 7,943
- 5
- 48
- 81
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,652
- 3
- 22
- 41
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,234
- 3
- 37
- 64
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,068
- 7
- 11