40

This has been asked elsewhere online to no avail. Is there any way in Android to display the numeric soft keyboard when focusing on an EditText, but still allow any text to be entered?

I'd like to let the user enter quantities (e.g. "1 kg", "2 L"), so just setting inputType="number" won't work.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
Lyudmil
  • 1,163
  • 2
  • 13
  • 21
  • There are lots of answers to this on many related posts, but unfortunately *it is not currently possible* using `inputType` or `setRawInputType()` if you want your code to work on "most devices" – Richard Le Mesurier Nov 25 '15 at 05:49

6 Answers6

33

Add the following line of code, and it will do the trick :)

editText.setRawInputType(Configuration.KEYBOARD_QWERTY);

This will show the the numeric keypad first, but also allows you to enter free text.

More information here.

Community
  • 1
  • 1
rDroid
  • 4,875
  • 3
  • 25
  • 30
8

This may be device dependant but have you tried:

 android:inputType="phone"

All Input Types Link

in the EditText's xml , this gives you the number pad keyboard but then you can still switch to letter's if you want. (Atleast on my Nexus One).

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • 4
    If you specify inputType="number" you can also switch to letters, but pressing on letters has no effect. If I set inputType="phone", I cannot even switch to letters on my device, never mind actually typing text. Can you type letters into your phone field? – Lyudmil May 27 '11 at 15:09
7

Note that: setRawInputType(InputType.TYPE_CLASS_NUMBER);

has the desired effect on some devices but not others...

On htc it works fine however on galaxy tab II you only get the numeric keyboard and no way to switch back to alpha.

Mark
  • 1,360
  • 3
  • 20
  • 37
4

write the code in XML, android:numeric="integer" android:inputType="phone" android:digits="1234567890"

Mostafiz
  • 77
  • 1
2

It looks like the underlying question you're dealing with is: how can I allow the user to enter quantities?

One appropriate answer is: with a numeric input, paired with some form of category select for the unit. e.g. radio, dropdown, or spinner. This is probably easier to use and also saves you the headache of having to validate your input every time.

You could also just have iron cojones and write a custom soft keyboard.

Cheezmeister
  • 4,895
  • 3
  • 31
  • 37
-1

I tried many different combinations before I figured this out, but this appears to work correctly:

setRawInputType(InputType.TYPE_CLASS_NUMBER);

The key lies in the description for setRawInputType(int):

Directly change the content type integer of the text view, without modifying any other state.

twaddington
  • 11,607
  • 5
  • 35
  • 46