How do I restrict my EditText input to numerical (possibly decimal and signed) input?
I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.
I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.
Here is my current code (I need to do this programmatically):
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.
If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
, the EditText accepts all input (which isn't what I want...).
I've tried combining flags (in desperation to see if it would work):
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)
That didn't work either (the EditText accepted all input as usual).
So, how do I do this?


add a comment |
I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.
I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.
Here is my current code (I need to do this programmatically):
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.
If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
, the EditText accepts all input (which isn't what I want...).
I've tried combining flags (in desperation to see if it would work):
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)
That didn't work either (the EditText accepted all input as usual).
So, how do I do this?


Programmatically? So you would be against an XML layout solution?
– Phil
Aug 2 '11 at 22:51
2
I wouldn't be against one, as long as I can create an EditText on the fly.
– kibibyte
Aug 3 '11 at 1:54
add a comment |
I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.
I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.
Here is my current code (I need to do this programmatically):
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.
If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
, the EditText accepts all input (which isn't what I want...).
I've tried combining flags (in desperation to see if it would work):
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)
That didn't work either (the EditText accepted all input as usual).
So, how do I do this?


I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.
I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.
Here is my current code (I need to do this programmatically):
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.
If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
, the EditText accepts all input (which isn't what I want...).
I've tried combining flags (in desperation to see if it would work):
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)
That didn't work either (the EditText accepted all input as usual).
So, how do I do this?




edited May 23 '17 at 11:47
Community♦
11
11
asked Aug 2 '11 at 22:15
kibibytekibibyte
1,65752339
1,65752339
Programmatically? So you would be against an XML layout solution?
– Phil
Aug 2 '11 at 22:51
2
I wouldn't be against one, as long as I can create an EditText on the fly.
– kibibyte
Aug 3 '11 at 1:54
add a comment |
Programmatically? So you would be against an XML layout solution?
– Phil
Aug 2 '11 at 22:51
2
I wouldn't be against one, as long as I can create an EditText on the fly.
– kibibyte
Aug 3 '11 at 1:54
Programmatically? So you would be against an XML layout solution?
– Phil
Aug 2 '11 at 22:51
Programmatically? So you would be against an XML layout solution?
– Phil
Aug 2 '11 at 22:51
2
2
I wouldn't be against one, as long as I can create an EditText on the fly.
– kibibyte
Aug 3 '11 at 1:54
I wouldn't be against one, as long as I can create an EditText on the fly.
– kibibyte
Aug 3 '11 at 1:54
add a comment |
9 Answers
9
active
oldest
votes
Try using TextView.setRawInputType() it corresponds to the android:inputType
attribute.
How exactly would I invoke this? Do I just invoke the calls in a chain?
– kibibyte
Aug 3 '11 at 1:50
44
using your example code:edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
– Dan S
Aug 3 '11 at 4:02
Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.
– Dan S
Aug 3 '11 at 4:19
2
What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?
– Maxrunner
Dec 9 '13 at 19:23
Check the InputType Reference for more details.
– Dan S
Dec 9 '13 at 23:40
|
show 1 more comment
There's no reason to use setRawInputType()
, just use setInputType()
. However, you have to combine the class and flags with the OR operator:
edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
Will give an uptick if you show the xml code as well for setting those flags.
– Matt Stevens
Jan 2 '14 at 1:44
This is not sufficient. You can still type multiple decimals. That is not a valid number.
– Andrew
Oct 9 '14 at 19:40
4
Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?
– DiscDev
Sep 17 '15 at 19:36
damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man
– Nasz Njoka Sr.
Mar 2 '16 at 11:19
1
note that the signed flag is not needed if you don't want to allow negative values
– Bart Burg
Jun 26 '18 at 14:04
|
show 1 more comment
The best way to do that programmatically is using the next method:
public static DigitsKeyListener getInstance (boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.
This solve the problem about the many '.' in EditText
editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.
add a comment |
put this line in xml
android:inputType="number|numberDecimal"
simple and good one
– Shylendra Madda
Apr 13 '17 at 7:03
add a comment |
Use this. Works fine
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
EDIT
kotlin version
fun EditText.onlyNumbers() {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
keyListener = DigitsKeyListener.getInstance("0123456789")
}
1
It's That i want, Thank you
– AndroSco
Nov 6 '17 at 12:17
May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 4:14
@LeonZaii allow only typing "1" and check that string length divisible by two
– V. Kalyuzhnyu
Oct 31 '18 at 6:17
noted. Thanks @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 8:38
add a comment |
Hi All I also had this problem. I use C# with Xamarin
Just a slight note that I hope someone else as well.
I have tried multiple of the methods mentioned here.
edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);
was the closest but still did not work. As Ralf mentioned, you could use
edit.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL |
InputType.TYPE_NUMBER_FLAG_SIGNED);
However in my C# I did not have this. instead you do it as follow:
edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;
Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!
Non programatically I manage this with justandroid:inputType="numberDecimal"
– Michael Gabay
Sep 19 '17 at 16:08
add a comment |
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);
add a comment |
TO set the input type of EditText as decimal use this code:
EditText edit = new EditText(this);
edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
add a comment |
my solution:`
public void onTextChanged(CharSequence s, int start, int before, int count) {
char ch=s.charAt(start + count - 1);
if (Character.isLetter(ch)) {
s=s.subSequence(start, count-1);
edittext.setText(s);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6919360%2fhow-do-i-restrict-my-edittext-input-to-numerical-possibly-decimal-and-signed-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try using TextView.setRawInputType() it corresponds to the android:inputType
attribute.
How exactly would I invoke this? Do I just invoke the calls in a chain?
– kibibyte
Aug 3 '11 at 1:50
44
using your example code:edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
– Dan S
Aug 3 '11 at 4:02
Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.
– Dan S
Aug 3 '11 at 4:19
2
What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?
– Maxrunner
Dec 9 '13 at 19:23
Check the InputType Reference for more details.
– Dan S
Dec 9 '13 at 23:40
|
show 1 more comment
Try using TextView.setRawInputType() it corresponds to the android:inputType
attribute.
How exactly would I invoke this? Do I just invoke the calls in a chain?
– kibibyte
Aug 3 '11 at 1:50
44
using your example code:edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
– Dan S
Aug 3 '11 at 4:02
Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.
– Dan S
Aug 3 '11 at 4:19
2
What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?
– Maxrunner
Dec 9 '13 at 19:23
Check the InputType Reference for more details.
– Dan S
Dec 9 '13 at 23:40
|
show 1 more comment
Try using TextView.setRawInputType() it corresponds to the android:inputType
attribute.
Try using TextView.setRawInputType() it corresponds to the android:inputType
attribute.
answered Aug 2 '11 at 22:21
Dan SDan S
8,14723042
8,14723042
How exactly would I invoke this? Do I just invoke the calls in a chain?
– kibibyte
Aug 3 '11 at 1:50
44
using your example code:edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
– Dan S
Aug 3 '11 at 4:02
Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.
– Dan S
Aug 3 '11 at 4:19
2
What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?
– Maxrunner
Dec 9 '13 at 19:23
Check the InputType Reference for more details.
– Dan S
Dec 9 '13 at 23:40
|
show 1 more comment
How exactly would I invoke this? Do I just invoke the calls in a chain?
– kibibyte
Aug 3 '11 at 1:50
44
using your example code:edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
– Dan S
Aug 3 '11 at 4:02
Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.
– Dan S
Aug 3 '11 at 4:19
2
What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?
– Maxrunner
Dec 9 '13 at 19:23
Check the InputType Reference for more details.
– Dan S
Dec 9 '13 at 23:40
How exactly would I invoke this? Do I just invoke the calls in a chain?
– kibibyte
Aug 3 '11 at 1:50
How exactly would I invoke this? Do I just invoke the calls in a chain?
– kibibyte
Aug 3 '11 at 1:50
44
44
using your example code:
edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
– Dan S
Aug 3 '11 at 4:02
using your example code:
edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
– Dan S
Aug 3 '11 at 4:02
Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.
– Dan S
Aug 3 '11 at 4:19
Hey its ok, I just picked the setter associated with the android input type. I'm happy you found it and I was close. Yep, its just a bit mask with flags, not all of them but many are.
– Dan S
Aug 3 '11 at 4:19
2
2
What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?
– Maxrunner
Dec 9 '13 at 19:23
What was the solution? using any of these i still get keys i don't want like "*","#", and parentesis, etc...i only want numbers and -/+/. chars, how to do this?
– Maxrunner
Dec 9 '13 at 19:23
Check the InputType Reference for more details.
– Dan S
Dec 9 '13 at 23:40
Check the InputType Reference for more details.
– Dan S
Dec 9 '13 at 23:40
|
show 1 more comment
There's no reason to use setRawInputType()
, just use setInputType()
. However, you have to combine the class and flags with the OR operator:
edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
Will give an uptick if you show the xml code as well for setting those flags.
– Matt Stevens
Jan 2 '14 at 1:44
This is not sufficient. You can still type multiple decimals. That is not a valid number.
– Andrew
Oct 9 '14 at 19:40
4
Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?
– DiscDev
Sep 17 '15 at 19:36
damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man
– Nasz Njoka Sr.
Mar 2 '16 at 11:19
1
note that the signed flag is not needed if you don't want to allow negative values
– Bart Burg
Jun 26 '18 at 14:04
|
show 1 more comment
There's no reason to use setRawInputType()
, just use setInputType()
. However, you have to combine the class and flags with the OR operator:
edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
Will give an uptick if you show the xml code as well for setting those flags.
– Matt Stevens
Jan 2 '14 at 1:44
This is not sufficient. You can still type multiple decimals. That is not a valid number.
– Andrew
Oct 9 '14 at 19:40
4
Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?
– DiscDev
Sep 17 '15 at 19:36
damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man
– Nasz Njoka Sr.
Mar 2 '16 at 11:19
1
note that the signed flag is not needed if you don't want to allow negative values
– Bart Burg
Jun 26 '18 at 14:04
|
show 1 more comment
There's no reason to use setRawInputType()
, just use setInputType()
. However, you have to combine the class and flags with the OR operator:
edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
There's no reason to use setRawInputType()
, just use setInputType()
. However, you have to combine the class and flags with the OR operator:
edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
answered Mar 28 '12 at 14:30
RalfRalf
10.3k94056
10.3k94056
Will give an uptick if you show the xml code as well for setting those flags.
– Matt Stevens
Jan 2 '14 at 1:44
This is not sufficient. You can still type multiple decimals. That is not a valid number.
– Andrew
Oct 9 '14 at 19:40
4
Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?
– DiscDev
Sep 17 '15 at 19:36
damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man
– Nasz Njoka Sr.
Mar 2 '16 at 11:19
1
note that the signed flag is not needed if you don't want to allow negative values
– Bart Burg
Jun 26 '18 at 14:04
|
show 1 more comment
Will give an uptick if you show the xml code as well for setting those flags.
– Matt Stevens
Jan 2 '14 at 1:44
This is not sufficient. You can still type multiple decimals. That is not a valid number.
– Andrew
Oct 9 '14 at 19:40
4
Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?
– DiscDev
Sep 17 '15 at 19:36
damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man
– Nasz Njoka Sr.
Mar 2 '16 at 11:19
1
note that the signed flag is not needed if you don't want to allow negative values
– Bart Burg
Jun 26 '18 at 14:04
Will give an uptick if you show the xml code as well for setting those flags.
– Matt Stevens
Jan 2 '14 at 1:44
Will give an uptick if you show the xml code as well for setting those flags.
– Matt Stevens
Jan 2 '14 at 1:44
This is not sufficient. You can still type multiple decimals. That is not a valid number.
– Andrew
Oct 9 '14 at 19:40
This is not sufficient. You can still type multiple decimals. That is not a valid number.
– Andrew
Oct 9 '14 at 19:40
4
4
Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?
– DiscDev
Sep 17 '15 at 19:36
Sometimes, Android makes me pull out my hair. Why is something so simple so incredibly hard and bug-prone?
– DiscDev
Sep 17 '15 at 19:36
damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man
– Nasz Njoka Sr.
Mar 2 '16 at 11:19
damn tried every thing... now i wonder in a single or static dynamic edittext it works with a simple one statement inputtype, with multiple dynamic et (et in an array) it does not i had to come here for the rescue.. thanks man
– Nasz Njoka Sr.
Mar 2 '16 at 11:19
1
1
note that the signed flag is not needed if you don't want to allow negative values
– Bart Burg
Jun 26 '18 at 14:04
note that the signed flag is not needed if you don't want to allow negative values
– Bart Burg
Jun 26 '18 at 14:04
|
show 1 more comment
The best way to do that programmatically is using the next method:
public static DigitsKeyListener getInstance (boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.
This solve the problem about the many '.' in EditText
editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.
add a comment |
The best way to do that programmatically is using the next method:
public static DigitsKeyListener getInstance (boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.
This solve the problem about the many '.' in EditText
editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.
add a comment |
The best way to do that programmatically is using the next method:
public static DigitsKeyListener getInstance (boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.
This solve the problem about the many '.' in EditText
editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.
The best way to do that programmatically is using the next method:
public static DigitsKeyListener getInstance (boolean sign, boolean decimal)
Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.
This solve the problem about the many '.' in EditText
editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); // decimals and positive/negative numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); // positive decimals numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); // positive integer numbers.
editText.setKeyListener(DigitsKeyListener.getInstance(true,false)); // positive/negative integer numbers.
answered Sep 4 '15 at 12:13


SerSánGalSerSánGal
429415
429415
add a comment |
add a comment |
put this line in xml
android:inputType="number|numberDecimal"
simple and good one
– Shylendra Madda
Apr 13 '17 at 7:03
add a comment |
put this line in xml
android:inputType="number|numberDecimal"
simple and good one
– Shylendra Madda
Apr 13 '17 at 7:03
add a comment |
put this line in xml
android:inputType="number|numberDecimal"
put this line in xml
android:inputType="number|numberDecimal"
answered Jan 19 '15 at 11:48
Qutbuddin BohraQutbuddin Bohra
5811620
5811620
simple and good one
– Shylendra Madda
Apr 13 '17 at 7:03
add a comment |
simple and good one
– Shylendra Madda
Apr 13 '17 at 7:03
simple and good one
– Shylendra Madda
Apr 13 '17 at 7:03
simple and good one
– Shylendra Madda
Apr 13 '17 at 7:03
add a comment |
Use this. Works fine
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
EDIT
kotlin version
fun EditText.onlyNumbers() {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
keyListener = DigitsKeyListener.getInstance("0123456789")
}
1
It's That i want, Thank you
– AndroSco
Nov 6 '17 at 12:17
May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 4:14
@LeonZaii allow only typing "1" and check that string length divisible by two
– V. Kalyuzhnyu
Oct 31 '18 at 6:17
noted. Thanks @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 8:38
add a comment |
Use this. Works fine
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
EDIT
kotlin version
fun EditText.onlyNumbers() {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
keyListener = DigitsKeyListener.getInstance("0123456789")
}
1
It's That i want, Thank you
– AndroSco
Nov 6 '17 at 12:17
May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 4:14
@LeonZaii allow only typing "1" and check that string length divisible by two
– V. Kalyuzhnyu
Oct 31 '18 at 6:17
noted. Thanks @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 8:38
add a comment |
Use this. Works fine
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
EDIT
kotlin version
fun EditText.onlyNumbers() {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
keyListener = DigitsKeyListener.getInstance("0123456789")
}
Use this. Works fine
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
EDIT
kotlin version
fun EditText.onlyNumbers() {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED
keyListener = DigitsKeyListener.getInstance("0123456789")
}
edited Nov 20 '18 at 10:25
answered Sep 29 '17 at 19:00


V. KalyuzhnyuV. Kalyuzhnyu
1,69822025
1,69822025
1
It's That i want, Thank you
– AndroSco
Nov 6 '17 at 12:17
May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 4:14
@LeonZaii allow only typing "1" and check that string length divisible by two
– V. Kalyuzhnyu
Oct 31 '18 at 6:17
noted. Thanks @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 8:38
add a comment |
1
It's That i want, Thank you
– AndroSco
Nov 6 '17 at 12:17
May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 4:14
@LeonZaii allow only typing "1" and check that string length divisible by two
– V. Kalyuzhnyu
Oct 31 '18 at 6:17
noted. Thanks @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 8:38
1
1
It's That i want, Thank you
– AndroSco
Nov 6 '17 at 12:17
It's That i want, Thank you
– AndroSco
Nov 6 '17 at 12:17
May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 4:14
May I know how to allow them to type 11 numbers only? @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 4:14
@LeonZaii allow only typing "1" and check that string length divisible by two
– V. Kalyuzhnyu
Oct 31 '18 at 6:17
@LeonZaii allow only typing "1" and check that string length divisible by two
– V. Kalyuzhnyu
Oct 31 '18 at 6:17
noted. Thanks @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 8:38
noted. Thanks @V.Kalyuzhnyu
– Leon Zaii
Oct 31 '18 at 8:38
add a comment |
Hi All I also had this problem. I use C# with Xamarin
Just a slight note that I hope someone else as well.
I have tried multiple of the methods mentioned here.
edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);
was the closest but still did not work. As Ralf mentioned, you could use
edit.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL |
InputType.TYPE_NUMBER_FLAG_SIGNED);
However in my C# I did not have this. instead you do it as follow:
edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;
Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!
Non programatically I manage this with justandroid:inputType="numberDecimal"
– Michael Gabay
Sep 19 '17 at 16:08
add a comment |
Hi All I also had this problem. I use C# with Xamarin
Just a slight note that I hope someone else as well.
I have tried multiple of the methods mentioned here.
edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);
was the closest but still did not work. As Ralf mentioned, you could use
edit.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL |
InputType.TYPE_NUMBER_FLAG_SIGNED);
However in my C# I did not have this. instead you do it as follow:
edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;
Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!
Non programatically I manage this with justandroid:inputType="numberDecimal"
– Michael Gabay
Sep 19 '17 at 16:08
add a comment |
Hi All I also had this problem. I use C# with Xamarin
Just a slight note that I hope someone else as well.
I have tried multiple of the methods mentioned here.
edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);
was the closest but still did not work. As Ralf mentioned, you could use
edit.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL |
InputType.TYPE_NUMBER_FLAG_SIGNED);
However in my C# I did not have this. instead you do it as follow:
edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;
Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!
Hi All I also had this problem. I use C# with Xamarin
Just a slight note that I hope someone else as well.
I have tried multiple of the methods mentioned here.
edittext1.SetRawInputType(Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal);
was the closest but still did not work. As Ralf mentioned, you could use
edit.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL |
InputType.TYPE_NUMBER_FLAG_SIGNED);
However in my C# I did not have this. instead you do it as follow:
edittext1.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;
Please note that the ORDER of these is important. If you put Decimal first, it will still allow you to type any Characters, where If Numer is first it is only numeric, but allows a decimal seperator!
answered Jun 17 '16 at 6:03
Marius Van WykMarius Van Wyk
514
514
Non programatically I manage this with justandroid:inputType="numberDecimal"
– Michael Gabay
Sep 19 '17 at 16:08
add a comment |
Non programatically I manage this with justandroid:inputType="numberDecimal"
– Michael Gabay
Sep 19 '17 at 16:08
Non programatically I manage this with just
android:inputType="numberDecimal"
– Michael Gabay
Sep 19 '17 at 16:08
Non programatically I manage this with just
android:inputType="numberDecimal"
– Michael Gabay
Sep 19 '17 at 16:08
add a comment |
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);
add a comment |
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);
add a comment |
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);
edited Feb 22 '13 at 18:11
answered Feb 21 '13 at 17:06


Yasin HassanienYasin Hassanien
3,33111513
3,33111513
add a comment |
add a comment |
TO set the input type of EditText as decimal use this code:
EditText edit = new EditText(this);
edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
add a comment |
TO set the input type of EditText as decimal use this code:
EditText edit = new EditText(this);
edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
add a comment |
TO set the input type of EditText as decimal use this code:
EditText edit = new EditText(this);
edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
TO set the input type of EditText as decimal use this code:
EditText edit = new EditText(this);
edit.SetRawInputType(Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
edited Jan 4 '14 at 6:28
answered Jan 4 '14 at 2:16
fulgenfulgen
962212
962212
add a comment |
add a comment |
my solution:`
public void onTextChanged(CharSequence s, int start, int before, int count) {
char ch=s.charAt(start + count - 1);
if (Character.isLetter(ch)) {
s=s.subSequence(start, count-1);
edittext.setText(s);
}
add a comment |
my solution:`
public void onTextChanged(CharSequence s, int start, int before, int count) {
char ch=s.charAt(start + count - 1);
if (Character.isLetter(ch)) {
s=s.subSequence(start, count-1);
edittext.setText(s);
}
add a comment |
my solution:`
public void onTextChanged(CharSequence s, int start, int before, int count) {
char ch=s.charAt(start + count - 1);
if (Character.isLetter(ch)) {
s=s.subSequence(start, count-1);
edittext.setText(s);
}
my solution:`
public void onTextChanged(CharSequence s, int start, int before, int count) {
char ch=s.charAt(start + count - 1);
if (Character.isLetter(ch)) {
s=s.subSequence(start, count-1);
edittext.setText(s);
}
answered Dec 7 '18 at 17:57


Meh yitMeh yit
141
141
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6919360%2fhow-do-i-restrict-my-edittext-input-to-numerical-possibly-decimal-and-signed-i%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Programmatically? So you would be against an XML layout solution?
– Phil
Aug 2 '11 at 22:51
2
I wouldn't be against one, as long as I can create an EditText on the fly.
– kibibyte
Aug 3 '11 at 1:54