2

I have a Webview and I load on it an url. This url has a form but I don´t want to show the Android´s keyboard when the user clicks to fill the form. I have tried this:

In the activity:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

In the manifest:

 android:windowSoftInputMode="stateHidden"

In the layout:

android:descendantFocusability="blocksDescendants"

and on webview´s properties:

android:focusable="false" 
android:focusableInTouchMode="true"

Nothing works for me, is there a solution? Thanks

My code is:

mWebview  = new WebView(this);


            mWebview.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {


                    if (url.startsWith("tel:")) {
                        //initiateCall(url);
                        return true;
                    }
                    if (url.startsWith("mailto:")) {
                        //sendEmail(url.substring(7));
                        return true;
                    }


                    return false;
                }


            });




            mWebview.getSettings().setJavaScriptEnabled(true);

            mWebview .loadUrl("http://www.myweb.com");
            setContentView(mWebview );
  • InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(webview.getWindowToken(), 0); just try not sure – saeed Mar 11 '16 at 11:33
  • http://stackoverflow.com/questions/17672116/how-disable-softkeyboard-in-webview hope helps you – saeed Mar 11 '16 at 11:40
  • did you try my solution below? In my case you can not select any input on the web-page, therefore the keyboard will not appear. – yital9 Mar 12 '16 at 12:04
  • Where can I put your code in my code? Can you refresh your answer using my code? Thanks very much – To TheLimit Mar 12 '16 at 12:10
  • I have already in my code mWebview.setWebViewClient(new WebViewClient(), can I put your onPageFinished method inside? – To TheLimit Mar 12 '16 at 13:41
  • I get an error in evaluatejavascript – To TheLimit Mar 12 '16 at 13:55

3 Answers3

0

I am not sure but you can do something like:

mWebView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            hideSoftKeyboard(View v);
            return false;  
        }     
}); 

And hiding part:

public void hideSoftKeyboard(View v) {
    Activity activity = (Activity) v.getContext();
    InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

See, if this works..

Chintan Soni
  • 22,543
  • 24
  • 96
  • 158
  • On `public boolean onTouch(View v, MotionEvent event) {hideSoftKeyboard(View v); etc etc...` remove View, it becomes: `public boolean onTouch(View v, MotionEvent event) hideSoftKeyboard(v);` – Bonatti Mar 22 '16 at 18:21
0

you can try to add a focus listener and hide the keyboad when the focus change

mWebview.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        //hide keyboard
        InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if(null != inputManager && null != getCurrentFocus()) {
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

    }
});
br00
  • 1,114
  • 8
  • 18
0

It's a little bit tricky, but generally the programming is such itself ;) The main idea, that you make with javascript all your editable elements on the web page read only. In my example I made it for inputs, but generally you can select everything what you want.

It should work:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WebView webView  = new WebView(this);
        setContentView(webView);

        webView.getSettings().setJavaScriptEnabled(true);

        final String js = "javascript: var allInputs = document.getElementsByTagName('input'); for (var i = 0, len = allInputs.length; i < len; ++i) { allInputs[i].readOnly = true;}";
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                if (Build.VERSION.SDK_INT >= 19) {
                    view.evaluateJavascript(js, new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) { }
                    });
                } else {
                    view.loadUrl(js);
                }
            }
        });
        webView.loadUrl("https://www.google.de/");
    }
}
yital9
  • 6,038
  • 13
  • 37
  • 51