Skip to content

Instantly share code, notes, and snippets.

@sssurvey
Created August 14, 2019 20:36
Show Gist options
  • Save sssurvey/c1bc88b9c196ca11fc31599e258e2441 to your computer and use it in GitHub Desktop.
Save sssurvey/c1bc88b9c196ca11fc31599e258e2441 to your computer and use it in GitHub Desktop.
hide soft keyboard on Android WebView 1
function hide_soft_keyboard(input_box) {
input_box.addEventListener("click", function() {
input_box.blur(); // very important call
input_box.focus();// very important call x2
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
/*
Normal onCreate stuff
*/
ExtendedWebView webView = findViewById(R.id.webview_id);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavascript(), "_my_javascript");
}
@Override 
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
hideKeyboard();
return super.onCreateInputConnection(outAttrs);
}
private void hideKeyboard() {
post(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) imm.hideSoftInputFromWindow(getRootView().getWindowToken(), 0);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment