Webview html input form not showing/allowing keyboard

http://stackoverflow.com/questions/9262092/webview-html-input-form-not-showing-allowing-keyboard





I need some help with showing/allowing the keyboard to show and input.

My basic app has a main screen with buttons, on button click it opens a webview, one of my buttons opens a webview to an HTML page with an input form. When you click on an input field, the keyboard does not show and when you use the hardware keyboard on the emulator it just brings up chinese suggestions and does not input any text.

share | improve this question

 
Was this post useful to you?      

2 Answers

up vote 3 down vote accepted

As Brent suggested, following line will serve the purpose -

webView.requestFocus(View.FOCUS_DOWN);

this happens because on page loading webpage loses its focus so this line just bring the focus back to page.

For those who end up at this question, A detailed discussion on this topic is here





I need some help with showing/allowing the keyboard to show and input.

My basic app has a main screen with buttons, on button click it opens a webview, one of my buttons opens a webview to an HTML page with an input form. When you click on an input field, the keyboard does not show and when you use the hardware keyboard on the emulator it just brings up chinese suggestions and does not input any text.

share | improve this question

 
Was this post useful to you?      

2 Answers

up vote 3 down vote accepted

As Brent suggested, following line will serve the purpose -

webView.requestFocus(View.FOCUS_DOWN);

this happens because on page loading webpage loses its focus so this line just bring the focus back to page.

For those who end up at this question, A detailed discussion on this topic is here


I have an application with just a visible WebView component to it which is used to display some dynamically generated HTML (can run Javascript too). It's enabled for the WebView.

For a few pages I am trying to set the focus of one of the input text boxes after the page load has finished - not through Body onLoad(), but instead by a JS call after the page has finished loading i.e. in onPageFinished(). Most of the javascript executes fine

    webview.loadURL("javascript:JSMethod();");

But when I use the same call to do a document.getElementById('text1').focus() - the cursor does reach the element but the Soft Keyboard won't pop out. The same javascript code when executed from a button on the page does have the desired effect.

I am attaching the source code which has 3 buttons

  1. Focus Text1 - Put the cursor on the text1 and pops up the Softkeyboard.
  2. Java Focus Text1 - Calls Java to execute the same JS. Only shows the cursor there and doesn't pop out the keyboard
  3. Java Focus Text1 And Keyboard Open - Calls JS from Java and Forces Keyboard open.

My understanding is that the JS execution should work the same whether executed from the browser using a button/event or as sent from Java through WebView.loadURL().

Here's my Queries

  1. Am I missing something when using Button#2? That's how my current code is and I only see the cursor is set but the SoftKeyboard won't open.
  2. When I use logic as written in Button#3, I some times don't see the cursor on the field but gives me the desired effect and the cursor becomes visible when I type something in the popped up keyboard.
  3. Could the behavior I see in Button#2 be a bug in Android JS execution? Or could it be that theWebView doesn't have focus that's why only the cursor is displayed? I also triedwebview.requestFocus() - I can't write requestFocusOnTouch() as it's the only View I have and am expecting it's focused automatically.

The Java code which demos the behavior is

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.inputmethod.InputMethodManager;
    import android.webkit.JsResult;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class WebViewProjectTestJSHTMLFocusActivity extends Activity {

        Activity _current = null;
        WebView wv = null;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            _current = this;
            //setContentView(R.layout.main);
            String data = "<html><body>" +
                                "<script>function focusser() { document.getElementById(\"text1\").focus(); } </script>" +
                                "<script>function javaFocusser() { javautil.javaFocus(false); } </script>" +
                                "<script>function javaFocusserKeyboard() { javautil.javaFocus(true); } </script>" +
                                "Text 1<input type='text' id='text1'/><br/>" +
                                "Text 2<input type='text' id='text2'/><br/>" +
                                "<input type='button' value='Focus Text1' onClick='focusser()'/>" +
                                "<input type='button' value='Java Focus Text1' onClick='javaFocusser()'/>" +
                                "<input type='button' value='Java Focus Text1 And Keyboard Open' onClick='javaFocusserKeyboard()'/>" +
                            "</body></html>";
            wv = new WebView(this);
            wv.getSettings().setJavaScriptEnabled(true);

            // Set some HTML 
            wv.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);

            // Call back required after page load finished
            wv.setWebViewClient(new CustomWebViewClient());

            // Enable Alert calls  
            wv.setWebChromeClient(new CustomWebChromeClient());

            // For JS->Java calls
            wv.addJavascriptInterface(this, "javautil");

            setContentView(wv);
        }


        /**
         * Calls the same javascript and forces the keyboard to open 
         */
        public void javaFocus(final boolean shouldForceOpenKeyboard) {

            Thread t = new Thread("Java focusser thread") {
                public void run() {
                    wv.loadUrl("javascript:focusser();");
                    if(shouldForceOpenKeyboard) {
                        InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        mgr.showSoftInput(wv, InputMethodManager.SHOW_IMPLICIT);
                    }
                }
            };

            // Run on the View Thread.
            _current.runOnUiThread(t);
        }

        /**
         * Calls focus method after the page load is complete.
         */
        final class CustomWebViewClient
        extends WebViewClient {
            @Override
            public void onPageFinished(WebView view, String url) {
                // javaFocus(true);
                Log.d("TestExamples", "focusser call complete");
            }
        }

        final class CustomWebChromeClient
        extends WebChromeClient {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Log.d("TestExamples", "JS Alert :: " + message);
                return false;
            }
        }
    }

Solution Update 24-06-2011 To make this work, you need to usewv.requestFocus(View.FOCUS_DOWN) just before the actual JS focus call. I modified thejavaFocus() method above to the correct version below. Earlier when I mentioned that I was using requestFocus(), I was using that when the WebView was initialized in the method onCreate(). The primary difference is now we're forcing the WebView to get focus each time just before the Javascriptdocument.getElementById("text1").focus(); is executed.

public void javaFocus(final boolean shouldForceOpenKeyboard) {
    Thread t = new Thread("Java focusser thread") {
        public void run() {
            wv.requestFocus(View.FOCUS_DOWN);
            wv.loadUrl("javascript:focusser();");
            if(shouldForceOpenKeyboard) {
                InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.showSoftInput(wv, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    };

    // Run on the View Thread.
    _current.runOnUiThread(t);
}

Also to ensure that this issue wasn't fixed because of focus triggered through touch etc, I am using a background thread to initiate the javaFocus() after 5 seconds of WebView Displayed. The modifiedonCreate() is below.

..... More onCreate code before....
// Enable Alert calls  
wv.setWebChromeClient(new CustomWebChromeClient());

// For JS->Java calls
wv.addJavascriptInterface(this, "javautil");

setContentView(wv);

new Thread("After sometime Focus") {
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        javaFocus(true);
    }
}.start();
.... onCreate() ends after this.... 
share | improve this question

 
Thanks for the thorough explanation. I had the same problem but this solution did not work for me on Android 3.2. In the end I had to write a function that simulated an actual tap on the field. I don't know why your solution didn't work. The WebView is just chock full of quirks. –  Pre101  Feb 20 at 5:49
 
I didn't test this code in Android 3.x/4.x. But did you try to run this sample program in either of them? –  Swaroop  May 21 at 2:46
 
I'd love to know if you can use this approach to make sure that a type=number input actually gets a numeric keyboard. As far as I am getting, InputMethodManager can only open the standard keyboard layout (Qwerty) since it doesn't know which HTML element currently has focus. Using wv.requestFocus didn't help. –  Wytze  Jun 29 at 20:01
Was this post useful to you?      

1 Answer

up vote 1 down vote accepted

It could be that the webview doesn't have the application focus. Try executing;

wv.requestFocus(View.FOCUS_DOWN);
share | improve this answer




I need some help with showing/allowing the keyboard to show and input.

My basic app has a main screen with buttons, on button click it opens a webview, one of my buttons opens a webview to an HTML page with an input form. When you click on an input field, the keyboard does not show and when you use the hardware keyboard on the emulator it just brings up chinese suggestions and does not input any text.

share | improve this question

 
Was this post useful to you?      

2 Answers

up vote 3 down vote accepted

As Brent suggested, following line will serve the purpose -

webView.requestFocus(View.FOCUS_DOWN);

this happens because on page loading webpage loses its focus so this line just bring the focus back to page.

For those who end up at this question, A detailed discussion on this topic is here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值