63

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?

Community
  • 1
  • 1
kibibyte
  • 3,007
  • 5
  • 30
  • 40

11 Answers11

182

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);
Ralf
  • 14,655
  • 9
  • 48
  • 58
  • Will give an uptick if you show the xml code as well for setting those flags. – Matt Stevens Jan 02 '14 at 01:44
  • This is not sufficient. You can still type multiple decimals. That is not a valid number. – Andrew Oct 09 '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 02 '16 at 11:19
  • This is the right answer. setRawInputType actually doesn't work. – Flyview Oct 12 '16 at 19:47
  • 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
  • I had to use `setRawInputType` because of problems with not accepting delimiter when setting accepted characters programatically and problems with selectionStart cursor which cannot be moved. – Wojtek Apr 04 '20 at 10:54
71

Try using TextView.setRawInputType() it corresponds to the android:inputType attribute.

Dan S
  • 9,139
  • 3
  • 37
  • 48
  • How exactly would I invoke this? Do I just invoke the calls in a chain? – kibibyte Aug 03 '11 at 01:50
  • 58
    using your example code: `edit.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);` – Dan S Aug 03 '11 at 04: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 03 '11 at 04: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 09 '13 at 19:23
  • Check the [InputType Reference](http://developer.android.com/reference/android/text/InputType.html) for more details. – Dan S Dec 09 '13 at 23:40
  • This answer is actually incorrect. The user can still type spacebar if we use this. setInputType() is the way to go – Pranav Mahajan Sep 06 '18 at 19:25
22

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")
}
Vlad
  • 7,997
  • 3
  • 56
  • 43
14

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.
SerSánGal
  • 467
  • 4
  • 15
  • Nice! I can't even copy-and-paste bad characters in! (Yes, I know how to copy and paste on Android;) – Phlip Dec 19 '19 at 22:53
  • Now all I need to do is alert the user if they enter a `0`, which is still forbidden... – Phlip Dec 19 '19 at 22:55
13

put this line in xml

 android:inputType="number|numberDecimal"
Qutbuddin Bohra
  • 1,165
  • 1
  • 11
  • 29
5

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!

2
EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);
Yasin Hassanien
  • 4,055
  • 1
  • 21
  • 17
1

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);
fulgen
  • 181
  • 2
  • 2
  • 12
1

Try having this in your xml of Edit Text:

android:inputType="numberDecimal"
chia yongkang
  • 786
  • 10
  • 20
0

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);
    }
Meh yit
  • 14
  • 4
0

use setRawInputType and setKeyListener

editTextNumberPicker.setRawInputType(InputType.TYPE_CLASS_NUMBER | 
                   InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED );
editTextNumberPicker.setKeyListener(DigitsKeyListener.getInstance(false,true));//set decimals and positive numbers. 
Hossein Hajizadeh
  • 1,357
  • 19
  • 10