93

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,068
  • 3
  • 16
  • 27
ryandlf
  • 24,707
  • 34
  • 102
  • 158

5 Answers5

219

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
  • 22,602
  • 12
  • 167
  • 196
Whiler
  • 7,887
  • 3
  • 31
  • 55
  • 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
  • 5,286
  • 3
  • 35
  • 97
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,609
  • 2
  • 32
  • 44
11

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,095
  • 3
  • 17
  • 31
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,342
  • 17
  • 79
  • 118
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