7

Possible Duplicate:
EditText with number keypad by default, but allowing alphabetic characters

Wondering if it is possible to default to a numeric keyboard on focus for an edittext field but also allow users to input characters? None of the input types seem to support this. Thanks!

Community
  • 1
  • 1
andymal
  • 534
  • 1
  • 6
  • 11
  • You'll have to create a button somewhere to act like a switch to programmatically set the InputType. Here's a link to a similar SO question, check my answer for more info: http://stackoverflow.com/questions/8978627/numeric-keyboard-as-default/8980169#8980169 – bschultz Feb 07 '12 at 20:24
  • 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

1 Answers1

-1

I just tried to add a number text field in Android and it ran in the emulator fine. It defaults to bring up the number pad first, but you can switch to characters just as easy. So try to use a number text field.

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number" />
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • 1
    Risky - some IMEs might not allow text input if you set the input type to `number`. – EboMike Feb 07 '12 at 21:45
  • This is true, hopefully most IME dev's check that out though. – EGHDK Feb 07 '12 at 21:57
  • 1
    android:inputType="number" does not allow me to enter anything but numbers. Are you using a stock keyboard? – andymal Feb 07 '12 at 21:58
  • Yes, I'm using stock android keyboard in Android version 15. So, this just goes to prove that the code should work, but since there are so many other keyboards your safest bet would seem to keep the regular EditText box. Sorry! – EGHDK Feb 07 '12 at 22:00
  • @EGHDK do you happen to know which IME will allow me to do this? – andymal Feb 07 '12 at 22:04
  • If you are using the app for yourself then of course you could just switch to the stock android keyboard. If you want to make this a public app it seems the best and most logical choice would be to just keep it a regular EditText box. – EGHDK Feb 07 '12 at 22:12
  • @EGHDK: First of all, as a user I'd be unhappy if an app would force me to use an IME other than the one that I set up as the default. Besides, there is a "stock Android keyboard" for Android 1.5, Android 2.0, Android 4.0... not to mention the stock HTC keyboard, etc... – EboMike Feb 07 '12 at 22:16
  • Well... err uhm. That's why I said not to set EditText to anything special. Just keep the box as an EditText plain and simple. – EGHDK Feb 07 '12 at 22:20
  • Thx for good info. I want to set the numeric input to come up first because the field is numeric 90% of the time but also needs to allow text. Purpose is to make it easier and faster for customers to use. – andymal Feb 07 '12 at 22:48
  • Exactly. It is very appealing to have numbers come up when numbers are asked to be inputted. I guess since different keyboards have different settings for this it would be difficult to fully test. Wish you luck. Please let me know what you ended up going with. It may benefit me one day. – EGHDK Feb 07 '12 at 23:30
  • What about simulating a button press for the button to change to the numeric keyboard as soon as the keyboard is opened? Anyone think that would work? – andymal Feb 24 '12 at 19:40
  • I don't understand exactly what you mean. – EGHDK Feb 24 '12 at 21:16