How do I restrict my EditText input to numerical (possibly decimal and signed) input?

AndroidAndroid Edittext

Android Problem Overview


I have read https://stackoverflow.com/questions/5200689/android-limiting-edittext-to-numbers and https://stackoverflow.com/questions/1119583/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?

Android Solutions


Solution 1 - Android

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);

Solution 2 - Android

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

Solution 3 - Android

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")
}

Solution 4 - Android

put this line in xml

 android:inputType="number|numberDecimal"

Solution 5 - Android

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.

Solution 6 - Android

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!

Solution 7 - Android

EditText edit = new EditText(this);

edit.setHorizontallyScrolling(true);
edit.setInputType(EditorInfo.TYPE_NUMBER_FLAG_SIGNED|EditorInfo.TYPE_CLASS_NUMBER);

Solution 8 - Android

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);

Solution 9 - Android

Try having this in your xml of Edit Text:

android:inputType="numberDecimal"

Solution 10 - Android

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);
    }

Solution 11 - Android

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. 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkibibyteView Question on Stackoverflow
Solution 1 - AndroidRalfView Answer on Stackoverflow
Solution 2 - AndroidDan SView Answer on Stackoverflow
Solution 3 - AndroidVladView Answer on Stackoverflow
Solution 4 - AndroidQutbuddin BohraView Answer on Stackoverflow
Solution 5 - AndroidSerSánGalView Answer on Stackoverflow
Solution 6 - AndroidMarius Van WykView Answer on Stackoverflow
Solution 7 - AndroidYasin HassanienView Answer on Stackoverflow
Solution 8 - AndroidfulgenView Answer on Stackoverflow
Solution 9 - Androidchia yongkangView Answer on Stackoverflow
Solution 10 - AndroidMeh yitView Answer on Stackoverflow
Solution 11 - AndroidHossein HajizadehView Answer on Stackoverflow