How to programmatically open Android soft keyboard according to the html input type in android webview

1.6k Views Asked by At

I want to show android soft keyboard on page load and programmatically focusing a input field.

$('#field').focus();

As far as I researched , this cannot be done other than user generated events , like click event.

So I tried a workaround to focus the input field , and show android keyboard manually by using this code.

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.toggleSoftInputFromWindow(myWebView.getWindowToken() , InputMethodManager.SHOW_IMPLICIT , 0);

Keyboard is successfully showing and input field is also getting focus. But the problem is , it is only showing the standard keyboard. I want to show the keyboard according to the input type specified.

Like if input field is:

<input type="number" id="field" name="field"/> 

Above workaround is showing standard text keyboard , but I want to show a numeric keyboard according to the type in input field.

2

There are 2 best solutions below

0
On

i've searched a long time before i found solution.

Add this :

imm.restartInput(view); 

after showSoftInput.

This will "recalculate" the kind of keyboard to show regarding focused input

1
On

Try this:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);