Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Soft-keyboard type in a custom HTML page

Is there any way to specify which soft keyboard layout (I need only numbers) is shown when the user taps on a text input field in an HTML page?

I can change the HTML page and the Javacode (I have an activity embedding a WebView). However, it must not affect other platforms if I change the HTML code (another browser/device should still interpret the input field properly and show a soft keyboard, not necessarly a numbers only keyboard)

Upvotes: 5

Views: 2138

Answers (3)

markiyanm
markiyanm

Reputation: 348

I've also seen that adding ' pattern="[0-9]*" ' to your input tag. Try:

<input type="number" size="4" maxlength="4" pattern="[0-9]*" ></input>

Upvotes: 1

Kumar Bibek
Kumar Bibek

Reputation: 9117

They seem to work differently in different devices and OS versions. It basically depends on the WebKit version that runs on a device. If it supports those tags, it will work, else, it ignores that tag.

On 2.3.4, on My Nexus S, type = number, gives a numeric keypad.

Upvotes: 1

Thilo-Alexander Ginkel
Thilo-Alexander Ginkel

Reputation: 6958

I did not try this, but in theory it should work:

Use the HTML5 type attribute for the <input> tag to declare the field as accepting only numbers. This will give the browser the hint to display a numeric keyboard. To have this only take effect on Android devices, consider emitting some Android-specific Javascript (which could either do the platform detection on the client through the user agent or which could alternatively only be sent by the server if that detects and Android device - again using the user agent header), which traverses through all input elements (maybe marked with a special class) and sets the type to number.

Upvotes: 4

Related Questions