95

I am essentially trying to set the digits value of an EditText programmatically. So far I have:

weightInput.setInputType(InputType.TYPE_CLASS_PHONE);
weightInput.setKeyListener(DigitsKeyListener.getInstance());

Which is fine, but I also want to be able to include a decimal place (.). Any ideas?

ehehhh
  • 1,066
  • 3
  • 16
  • 27
ryandlf
  • 27,155
  • 37
  • 106
  • 162

6 Answers6

226

Try this:

<EditText
    android:inputType="number"
    android:digits="0123456789."
/>

From Code:

weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789."));

But, it allows the user to include several "." See JoeyRA's answer for real numbers.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
Whiler
  • 7,998
  • 4
  • 32
  • 56
  • That is fine, but I want to do this programatically. Reason for this is because I want to reuse one layout in multiple situations as this digits is the only variable that changes constantly. Doing it in code is much more effective in my situation. – ryandlf Sep 04 '11 at 16:25
  • 2
    Thanks. I actually feel this answer deserves the checkmark because it is specific to what I asked for, but both solutions work. – ryandlf Sep 05 '11 at 06:41
  • @feresr, that's strange, because looking at `TextView` sources: `// If no input type was specified, we will default to generic text, since we can't tell the IME about the set of digits that was selected.` – Dmitry Gryazin Nov 14 '17 at 13:19
  • What is the use of `.` after 9? – Mitesh Shah Aug 23 '18 at 07:03
  • this doesn't seem to work. THe decimal doesn't show up on the keyboard. Infact, I can specify a subset of the numbers in the getInstance() call, and it still lets me type any number. – Stealth Rabbi Nov 21 '19 at 13:48
  • `setKeyListener` will limit the use of only digits (not letters) on software keyboards. On emulator's hardware keyboard works well. – CoolMind Dec 10 '19 at 10:18
  • 1
    @MiteshShah it allows for decimal place. – masterwok Sep 02 '20 at 17:26
34

Try this:

weightInput.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);           
weightInput.setKeyListener(DigitsKeyListener.getInstance(false,true));

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

user924
  • 8,146
  • 7
  • 57
  • 139
JoeyRA
  • 478
  • 4
  • 5
  • 1
    Just add a clarification: editText.setKeyListener(DigitsKeyListener.getInstance(true,true)); to enable decimals and negative numbers. editText.setKeyListener(DigitsKeyListener.getInstance(false,true)); to enable only positive decimals numbers. editText.setKeyListener(DigitsKeyListener.getInstance(false,false)); to enable only positive integers. – SerSánGal Sep 04 '15 at 12:04
  • 9
    Question: if you `setInputType` once and again, won't it override the second one to the first one? I think you should use `weightInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);` but I'm not 100% sure – Rafael Ruiz Muñoz Apr 04 '16 at 14:49
  • @RafaelRuizMuñoz of course it will override – user924 Sep 07 '21 at 09:40
  • this solution still allows to enter dot before you even start to enter digits – user924 Sep 07 '21 at 09:41
21

Use InputType.TYPE_NUMBER_FLAG_DECIMAL.

Also see: Input Types.

Ricky
  • 7,785
  • 2
  • 34
  • 46
12

if anyone still finding proper way, note that its setRawInputType() not setInputType()

val digits = "ABCDabcd" // or any characters you want to allow
editText.keyListener = DigitsKeyListener.getInstance(digits) 
editText.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME)
Asad
  • 1,241
  • 3
  • 19
  • 32
4

For IP address input ( Multiple dots and numbers )

try

<EditText
    android:id="@+id/ipBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/ipAddrHint"
    android:inputType="numberDecimal|number"
    android:digits="0123456789."
    android:textSize="30sp" />
VMAtm
  • 27,943
  • 17
  • 79
  • 125
NAGESH M H
  • 41
  • 2
  • 1
    Please note that hexadecimal IP addresses (with parts starting with 0x) are valid too, and domain name can be used instead of IP address. – Triang3l May 23 '13 at 18:00
0

None of the above solutions worked as expected. Digits in xml file still allow me to put "," in the input. Check for it, it's works for me:

edittext.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);