How do I default to numeric keyboard on EditText without forcing numeric input? [duplicate]

StackOverflow https://stackoverflow.com/questions/8776491

  •  14-04-2021
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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).

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.

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top