I have an EditText
in which I want only integer values to be inserted. Can somebody tell me which property I have to use?
-
1Please see this link: stackoverflow.com/a/70189238/12272687– MoriDec 1, 2021 at 18:09
-
Mori's comment is the best answer here. Have you considered answering this question with it, @Mori?– ChuckyApr 6 at 17:51
13 Answers
Add android:inputType="number"
as an XML attribute.
-
13
-
14Hi, in android 2.3.3, it is "numberSigned". Just wrote it down here, in case someone is seeking for it :)– PeakGenJan 23, 2013 at 15:22
-
1What if my editText appears in the dialog, and thus, I only declare it in .java code? i.e. the dialog editText does not appear in XML file. How can I still achieve this? Jun 20, 2013 at 7:41
-
1In addition, you could use
android:inputType="phone"
in place of that. But it seems a bit more "busy". Jul 21, 2013 at 2:48
For example:
<EditText
android:id="@+id/myNumber"
android:digits="0123456789."
android:inputType="numberDecimal"
/>
-
3
-
2I'm assuming the string should read
0123456789.
and not0123456780.
Feb 1, 2014 at 1:28 -
This works with android:inputType="number" as well! I needed it in order to prevent users from typing operators, parentheses, etc. on the number pad. What a life saver, man! Welcome to Costco; I love you! Jul 23, 2014 at 23:38
-
2This will not allow you to put negative numbers and at same time it will allow duplicate decimals.(ex: 1.1.1.....) Apr 7, 2016 at 9:40
-
1
In code, you could do
ed_ins.setInputType(InputType.TYPE_CLASS_NUMBER);
-
-
1Sourabh asked for "Only integer values to be inserted". This allows to input a lot more.– ZonJul 4, 2017 at 6:46
For only digits input use android:inputType="numberPassword"
along with editText.setTransformationMethod(null);
to remove auto-hiding of the number.
OR
android:inputType="phone"
For only digits input, I feel these couple ways are better than android:inputType="number"
. The limitation of mentioning "number" as inputType is that the keyboard allows to switch over to characters and also lets other special characters be entered. "numberPassword" inputType doesn't have those issues as the keyboard only shows digits. Even "phone" inputType works as the keyboard doesnt allow you to switch over to characters. But you can still enter couple special characters like +, /, N, etc.
android:inputType="numberPassword" with editText.setTransformationMethod(null);
inputType="phone"
inputType="number"
-
2This is Perfect!
android:inputType="numberPassword"
along witheditText.setTransformationMethod(null);
– coderpcMay 21, 2017 at 22:08 -
android:inputType="numberDecimal"
-
5This is great, but the Integer number system doesn't include any decimal numbers.– RileyEJan 4, 2013 at 20:02
-
@RileyE What do you mean by this? We just started using this for all our numeric inputs and I want to make sure we're not missing something. Thanks! Jan 9, 2019 at 17:43
-
<EditText
android:id="@+id/age"
android:numeric="integer"
/>
-
3android:numeric is deprecated since API 3, see answer to stackoverflow.com/questions/12625380/…– winne2Apr 15, 2016 at 11:44
Try the following code:
Edittext_name.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
It will allow you to enter just numbers. You cannot enter chars.
if you want to enter chars, not numbers, you can edit the values between the quotes inside getInstance
.
using the below can solve your problem better;
in xml:
<EditText
android:id="@+id/age"
android:inputType="numberDecimal|numberSigned" />
or //in activity inside etfield.addtextchangelistener
private String blockCharacterSet="+(/)N,*;#";//declare globally
try {
for (int i = 0; i < s.length(); i++) {
if (blockCharacterSet.contains(s.charAt(i) + "")) {
String corrected_settempvalue = arrivalsettemp.substring(0, arrivalsettemp.length() - 1);
et_ArrivalSetTemp.setText(corrected_settempvalue);
if (corrected_settempvalue.length() != 0)
et_ArrivalSetTemp.setSelection(corrected_settempvalue.length());
}
}
} catch (Exception d) {
d.printStackTrace();
}
If anyone want to use only number from 0 to 9 with imeOptions
enable then use below line in your EditText
android:inputType="number|none"
This will only allow number and if you click on done/next button of keyboard your focus will move to next field.
You can use it in XML
<EditText
android:id="@+id/myNumber"
android:digits="123"
android:inputType="number"
/>
or,
android:inputType="numberPassword" along with editText.setTransformationMethod(null); to remove auto-hiding of the number.
or,
android:inputType="phone"
Programmatically you can use
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
I need to catch pressing Enter on a keyboard with TextWatcher. But I found out that all numeric keyboards android:inputType="number" or "numberDecimal" or "numberPassword" e.t.c. don't allow me to catch Enter when user press it.
I tried android:digits="0123456789\n"
and all numeric keyboards started to work with Enter and TextWatcher.
So my way is:
android:digits="0123456789\n"
android:inputType="numberPassword"
plus editText.setTransformationMethod(null)
Thanks to barmaley and abhiank.
I don't know what the correct answer was in '13, but today it is:
myEditText.setKeyListener(DigitsKeyListener.getInstance(null, false, true)); // positive decimal numbers
You get everything, including the onscreen keyboard is a numeric keypad.
ALMOST everything. Espresso, in its infinite wisdom, lets typeText("...")
inexplicably bypass the filter and enter garbage...
-
3
-
the simplest for me
android:numeric="integer"
although this also more customize
android:digits="0123456789"
-
android:numeric is deprecated since API 3, see answer to stackoverflow.com/questions/12625380/…– winne2Apr 15, 2016 at 11:44