Owen Campbell
Owen Campbell

Reputation: 632

Configure android EditText to allow decimals and negatives

I have an Android EditText that I want to have the number keyboard come up.

If I set the android:inputType to numberSigned, I get the number keyboard and the ability to type in negatives. However this won't let me use decimals. If I use the numberDecimal inputType I can use decimals but not negatives.

How do you get the number keyboard with the ability to type in decimals and negatives?

Upvotes: 43

Views: 45566

Answers (6)

user2991150
user2991150

Reputation: 31

Tested, solution is

inputET.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);

Upvotes: 2

fmag
fmag

Reputation: 123

This worked for me programmatically.Below is the code :

editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);

Upvotes: 0

Nicholas Anderson
Nicholas Anderson

Reputation: 61

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="numberDecimal|numberSigned|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.

Upvotes: 3

Rukmal Dias
Rukmal Dias

Reputation: 3262

We can use,

edit_text.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);

if we need to use programmatically..

Upvotes: 17

kalpana c
kalpana c

Reputation: 2739

See this link may be it's help you http://developer.android.com/resources/articles/creating-input-method.html

and The possible values for the android:inputtype are:

•none
•text
•textCapCharacters
•textCapWords
•textCapSentences
•textAutoCorrect
•textAutoComplete
•textMultiLine
•textImeMultiLine
•textNoSuggestions
•textUri
•textEmailAddress
•textEmailSubject
•textShortMessage
•textLongMessage
•textPersonName
•textPostalAddress
•textPassword
•textVisiblePassword
•textWebEditText
•textFilter
•textPhonetic
•textWebEmailAddress
•textWebPassword
•number
•numberSigned
•numberDecimal
•numberPassword
•phone
•datetime
•date
•time

Upvotes: 7

Lalit Poptani
Lalit Poptani

Reputation: 67296

You are just missing this in your EditText,

android:inputType="numberDecimal|numberSigned"

Upvotes: 106

Related Questions