On Android webview when i execute javascript the sotfkeyboard disappear
android, android-softkeyboard, focus, javascript, webview
Solution
The easiest way of doing that is storing the javascript calls in a plugin or a JavascriptInterface and have repeating method in your javascript side executing it. That way `loadUrl` is never called.
Something like:
StringBuilder sb = null;
private final Object LOCK = new Object();
public void sendJavascript(String js){
synchronized(LOCK) {
if (sb == null){
sb = new StringBuilder();
sb.append("javascript:");
}
sb.append(js):
}
}
Then using a javascriptInterface:
private class JsInterface {
@JavascriptInterface
public String getJavascript(){
synchronized(LOCK) {
String javascriptToRun = sb.toString();
sb = new StringBuilder();
sb.append("javascript:");
return javascriptToRun;
}
}
}
Add your javascript interface to your webview:
JsInterface jsInterface = new JavascriptInterface();
webview.addJavascriptInterface(jsInterface, "JsInterface");
And on your javascript code set a timed interval for retrieving the stored function.
EDIT
I just found this answer, it shows mine has a big problem, the methods are not synchronized. Meaning when you call `getJavascript()` our variable `sb` might not be ready to be read yet. I've added the relevant bit to the source above.
USE THE LOCKS or your code will be unreliable
Problem
In WebView when I execute a JavaScript through webview.loadUrl(), the softkeyboard disappears if it is visible. When I try to type some text in html text field, the softkeyboard disappears (if JavaScript is executed) and I'm unable to type the all text. The text field does not lose focus, so the prompt is still on the text field, but the softkeyboard goes down. Can someone tell me how fix that?