14

My question is this.

I can say in xml

android:digits="0123456789,"

But this time I've to it add trough java code.

customEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));

Doesn't work. Application gives me same result as android:InputType="number"

So is there any alternative way to say android:digits using java?

EDITED

So my question wasn't how to get android:InputType="number" using java.

My question is that how I can get android:digits="0123456789," to be used with java.

At this point I don't care if the user can see characters. I just want my edittext field to accept only numbers from 0 to 9 and the decimal comma (,).

Because of the customEdittext is really a customized edittext I can't use xml atm.

joonasj
  • 551
  • 2
  • 6
  • 19

5 Answers5

11

try customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
3
EditText ed;
ed.setInputType(InputType.TYPE_CLASS_NUMBER);

also docs may help

Andrew F
  • 1,712
  • 2
  • 15
  • 24
  • 1
    Well I don't want to use number. Because then I can't get the decimal comma in use. – joonasj Jul 17 '12 at 09:27
  • try to using inputfilter http://developer.android.com/reference/android/text/InputFilter.html customEditText.setFilter(yourInputFilter); – Andrew F Jul 17 '12 at 09:51
1

According to the Android documentation TYPE_NUMBER_FLAG_DECIMAL should work http://developer.android.com/reference/android/text/InputType.html

However it doesn't, if you use only TYPE_NUMBER_FLAG_DECIMAL the user will be able to input letters as well.

But by our luck, Haresh Chaudhary, provide the correct answer, both must be use, TYPE_CLASS_NUMBER and also TYPE_NUMBER_FLAG_DECIMAL. I try hes solution and It worked.

Allthough I used setInputType instead of setRawInput:

yourEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Opal
  • 81,889
  • 28
  • 189
  • 210
cutiko
  • 9,887
  • 3
  • 45
  • 59
0

Did you try customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER)?

Reference: InputType

MByD
  • 135,866
  • 28
  • 264
  • 277
Aswin Kumar
  • 5,158
  • 5
  • 34
  • 39
-1

Try to add android:inputType="number" in the xml layout to your EditText (link)

overbet13
  • 1,654
  • 1
  • 20
  • 36
  • Well this time I can't, because of the code. I've to do it using java only. And I wan't to have my edittext field to accept only numbers 0-9 and decimal comma (,). – joonasj Jul 17 '12 at 09:32