7

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.

Jr.
  • 71
  • 1
  • 4
  • I have similar issue on Android 3.0 (Motorola Zoom). On other devices with android 2.x it works normally. Did you find a solution? – Ilya Izhovkin Jun 15 '11 at 10:56

3 Answers3

5

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.

Joe B
  • 596
  • 4
  • 11
  • Thanks for your reply. I overridden this API but using "reflection" way to avoid the function call to hide the virtual keyboard. – Jr. Sep 27 '12 at 07:05
  • @Jr. could you explain what exactly have you done to fix this? Thanks. – Piotr Nov 12 '12 at 13:43
2

I have the same problem and haven't fully solved it yet.

I have been able to force the keyboard to show after calling webView.loadUrl, first you need to find out if the keyboard is showing, I used a modified version of this How to check visibility of software keyboard in Android?

then call

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(webview, InputMethodManager.SHOW_IMPLICIT);

But if like me you need to communicate with the JavaScript in the WebView after each key press then this approach causes the keyboard to flicker as it tries to animate up and down.
It also misses some of the input depending on how fast you type.

Community
  • 1
  • 1
SamHassan
  • 21
  • 2
1

In Android, executing javascript from WebView is one-way: calling WebView.loadUrl("javascript:sub(1,1)"); You can not get the return value directly.

There's a async way to get the JavaScript function return value: How to get return value from javascript in webview of android?

But in most cases, we need a sync method instead of the async way. For example, while click event is passed into Android WebView's onTouch method, we want to execute a JavaScript to reading the element information clicked on.

I have tried two solutions: 1. Use Java P/V variable to wrap a sync method from the async on, the solution looks like: How to get return value from javascript in webview of android?

The solution is totally not working, because wait method will block the execution of JavaScript and JavaScriptInterface. So if we want to get the result of a js method in a UI Thread. it's not possible.

  1. Use Java Reflection to get the Native Interface of stringByEvaluatingJavaScriptFromString. Exist stringByEvaluatingJavaScriptFromString for Android

Works very well, the only thing need to care is to initial reflection method when page loaded finished. I tried to initial the reflection inside constructor of WebView, but failed. I think the constructor is async method.

Community
  • 1
  • 1
Jonny Chen
  • 84
  • 3