I am making a calculator and I want to make sure that user is able to enter a "Negative Decimal Number" I see the option in the xml file separately for example in order to put the Decimal Number, the EditText's inputType must be numberDecimal and for negative number the inputType must be "numberSigned" but I need both. If I choose inputType as "text" then user is able to enter anything which throws an exception if it's not a number. I am not sure how to fix that either.
Asked
Active
Viewed 2.1k times
4 Answers
62
You can use both with the "|" operator. Just specify "numberSigned|numberDecimal".

André Diermann
- 2,725
- 23
- 28
-
1Be aware that some keyboards (Samsung stock for example) will always offer a dot as decimal separator even in countries that use a comma. There is no way around it until the keyboard is fixed. See here for my workaround: https://stackoverflow.com/a/51983392/238664 – Kevin Read Aug 23 '18 at 10:13
8
Add this in the layout of the EditText
:
android:digits="0123456789-."
You can specify more there, for example if you want the user to input sin()
functions.

g00dy
- 6,752
- 2
- 30
- 43
5
As @a11n says, you can use the pipe operator to specify both:
<EditText
...
android:inputType="numberSigned|numberDecimal"
... />
Valid numbers: positive and negative decimal and whole numbers.

David Miguel
- 12,154
- 3
- 66
- 68
2
Some phones have the decimal and negative in the same button and makes it unable to do negatives. I figured out a way you could separate the buttons by simply adding:
android:inputType="numberSigned|numberDecimal|textPersonName"
android:digits="0123456789.-"
That way you still have the number keyboard layout but the negative and decimal buttons will be separate and you cannot use any other digit that messes up the app.

MBT
- 21,733
- 19
- 84
- 102

Nicholas Anderson
- 41
- 2