0

I'd like to hide the Android native keyboard when I do click over an <input type=text> without plugins and without lost the focus of the input.

With JavaScript I can detect when the keyboard show or hide, but I'd like to hide when I want it.

The backbutton hides the keyboard when it is showing, so, I think that I could trigger the backbutton click event when I detect a showkeyboard event, but it doesn't work.

document.addEventListener("showkeyboard", 
    function(){ 
        alert("Keyboard is ON"); 
        $("backbutton").trigger('click'); // Doesn't work effect
    }, false);

document.addEventListener("hidekeyboard", 
    function(){ 
        alert("Keyboard is OFF");

    }, false);
1

2 Answers 2

0

first in your mainactivity import :

    import android.view.inputmethod.InputMethodManager;
    import android.webkit.JavascriptInterface;

Then add a javascriptinterface in onCreate Method :

   super.appView.addJavascriptInterface(new JSInterface(), "testInterface");

After that add the follwing code in Mainactivity class :

    @JavascriptInterface
    public void hideKeyboard() {
        runOnUiThread(new Runnable() {

            public void run() {
                try {
                    InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext().getSystemService(Service.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(<MainactivityClass>.this.getCurrentFocus().getWindowToken(), 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

Now from HTML, call the native hideKeyboard() method in the input box like this way :

    <input type="text" onfocus="window.testInterface.hideKeyboard()" />
0

Try using directly

document.getElementById('input_field_to_focus').focus();

This adds focus to the input field but does not invoke the soft keyboard. The keyboard open event is not triggered on Android unless it is from a click event.

Refer the link:http://code.google.com/p/android/issues/detail?id=27438

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.