Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard in HTML text-input disappear when WebView call 'loadUrl' again

Tags:

android

I use WebView for my Androind App. I got a problem and request a solution for help.

There is a textfield in the HTML page. When it gets 'focus' and then I call

          mWebView.setFocusableInTouchMode(true);

in Java code so that the Android soft-keyboard will pop-up to let me key in.

The problem is I need using multi-thread for some processes in Java and call

          mWebView.loadUrl(strJSCall); 

as callback to execute JavaScript function, but the keyboard gets hidden!

The way I try is to force the keyboard to show again. But how can the keyboard always show when 'loadUrl' is called? Dose anyone meet the same issue and solve it already?

Sincerely, Jr.

like image 738
Jr. Avatar asked Dec 27 '10 03:12

Jr.


1 Answers

The problem with loadUrl is that it interrupts the UI thread and will still close the input method. The only solid way to do this is to check what the cursor is on the webview, and override the default loadUrl behaviour on your webview.

Inside your custom webview:

@Override
public void loadUrl(String url) {
    HitTestResult testResult = this.getHitTestResult();
    if (url.startsWith("javascript:)" && testResult != null && testResult.getType() == HitTestResult.EDIT_TEXT_TYPE)
    {
            //Don't do anything right now, we have an active cursor on the EDIT TEXT
            //This should be Input Method Independent
    }
    else
    {
        super.loadUrl(url);
    }
}

This will prevent the native side from loading Javascript from firing when you have focus on a text field in webkit. It's not perfect but it avoids the mess of trying to figure out whether your text field is visible either by the resizing of the WebView. The Javascript executing in the webview should still work fine.

Again, if you need something to update while you're typing, you may have to find another way for Java to communicate with Javascript that doesn't block the UI thread. This is a hard problem that I still haven't solved properly yet.

like image 103
Joe B Avatar answered Oct 31 '22 17:10

Joe B