Specifically, how do I get it to pop up as the numbers entry screen but still be able to switch to letters? What I'd really like, ideally, is to have it act the same as if we had the xml property
<EditText
...
android:inputType="textShortMessage"/>
but come up on the number entry screen (the one you get by clicking "?123") on first showing, rather than the usual qwerty one. I've tried doing eg
<EditText
...
android:inputType="textShortMessage|number"/>
but all that does is default it to the phone-number-entry screen with no option to enter letters. Any ideas?
In Java code, use:
edittext.setRawInputType(InputType.TYPE_CLASS_NUMBER);
This discussion seems to indicate that you can use:
android:inputType="number"
to achieve what you are looking for. But I think the poster of that solution may have misunderstood the problem, as I believe that "number" will only allow (of course) numbers, and not allow letters. I am not able to test this at the moment, so I will have to speculate.
Alternately, you could have two EditTexts for your input: one EditText strictly numeric, and the other plain text, such that you could enter a quantity in the first field and a unit in the second field. I'm guessing that you are eventually parsing the input as a string anyhow, and in that case all you would have to do is concatenate the text before parsing.
Related
I know there are several posts about facilitating navigation between EditText fields. However, I can't get the suggested solutions to work and still have my EditTexts automatically have the first letter be capitalized. While setting the input type to "text" accomplishes what I want, if I set it to "textCapSentences", even using imeOptions, nextFocus, etc., I can't get the behavior I'm looking for.
This question already has answers here:
EditText with number keypad by default, but allowing alphabetic characters
(19 answers)
Closed 9 years ago.
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.
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.
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.
In my application I have an EditText (Actually it's an implementation of MultiAutoCompleteTextView, but I don't think that matters) in which the user can enter a formula to be calculated, although generally they will want to just enter an integer.
What I would really like is for the keyboard to open with the numeric keyboard showing, but allow them to change to text if they want.
As standard of course it shows the letters and allwos them to change to numbers, which is OK, but 90% of the time they will want the numbers.
I tried doing:
operandEditText.setInputType(InputType.TYPE_CLASS_NUMBER|
InputType.TYPE_NUMBER_FLAG_SIGNED|InputType.TYPE_CLASS_TEXT);
But the result was a numeric keypad with letters written on the number keys (like a phone keypad), and it didn't write letters anyway.
I'm also aware that I could add a button, spinner or something that changed the input mode of the widget, but the aim of this is to make the interface easier to use, and I'm not sure adding another control will achieve that.
Is there a way to make it do what I want?
I'm trying to devise an input type for a field which will accept credit card numbers.
I had been using inputType="number" - but that won't let people with hardware keyboards hit the space bar.. when they do it jumps to another field in the activity.
I'd like to allow users to use a space in their numbers if they want, or, at least, make it so that if users with a hardware keyboard hit the space when I'm only allowing numbers it won't leave the credit card number EditText.
Ideally I'd be able to implement some interface and have my own custom inputType, but I'm not sure that's possible.
Is it possible to allow numbers and spaces, while showing the numbers soft keyboard?
The best choise is InputType.TYPE_CLASS_DATETIME
How about android:inputType="phone" in your layout xml file?
I don't have a definitive answer for you but here are some resources I picked up:
I found this clue to "catch" hard key presses: "To intercept hard keys, override InputMethodService.onKeyDown()". Read from bottom heading "Intercepting hard key events".
If you can't find a way to intercept and deal with the "space" key, I would suggest using a normal TextView and manually opening the number keyboard rather than the qwerty one (Go here for more info on general Input Reference). You can then validate key pressed from the hard keyboard.
Sorry I couldn't give you a more solid answer!
This question already has answers here:
EditText with number keypad by default, but allowing alphabetic characters
(19 answers)
Closed 9 years ago.
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.
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.
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.