6

On some devices (mostly Samsung, but others too) and combinations of: Android version, WebView version (even with the evergreen WebView in Android 7) and keyboard, there are a number of issues:

  • keypress isn't fired
  • keydown and keyup always contain keyCode=229
  • keypress and input are fired but don't contain the key
  • textInput isn't fired
  • maxlength attribute isn't honored on input[type=text] while the user types (more than maxlength chars are allowed and the input is validated only when the form is submitted)

Is there a way to fix these issues?

gabrielmaldi
  • 2,157
  • 2
  • 23
  • 39

1 Answers1

7

I found that if you extend WebView and override onCreateInputConnection, all of those issues are fixed:

public class WebViewExtended extends WebView {
    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        // This line fixes some issues but introduces others, YMMV.
        // super.onCreateInputConnection(outAttrs);

        return new BaseInputConnection(this, false);
    }
}

Before overriding onCreateInputConnection:

before

After overriding onCreateInputConnection (g was pressed):

after

gabrielmaldi
  • 2,157
  • 2
  • 23
  • 39
  • Seriously: _thank you_ just about a million times . What did you mean by "fixes some issues but introduces others, YMMV."? – VH-NZZ Dec 07 '18 at 08:51
  • @VH-NZZ glad this helped you. Initially, without the `super` call, we found that sometimes the user would open a numeric keyboard in another app, and when returning to our app, only that same numeric keyboard would be opened for *any* field, even if for text fields. The `super` call fixed that for us. But it introduced a problem in some specific devices (can't recall which) where typing wouldn't work: the user would hit the keyboard and nothing happened. That's why your mileage may vary and you should test if this solution works for your specific scenario. – gabrielmaldi Dec 07 '18 at 19:14
  • Cheers for this explanation mate. This is a bit of a pain to manage but luckily the user base I'm target is around 20 and all use the same hardware. – VH-NZZ Dec 07 '18 at 20:09
  • Tried this and it wasn't working initially. Until I found this: https://medium.com/mobile-app-development-publication/managing-keyboard-on-webview-d2e89109d106 Point 4: You'll have to add `android:focusable="true"` and `android:focusableInTouchMode="true"`to your mark-up to make it work. At least at Android API Level 26 and above. – SqAR.org Aug 10 '20 at 16:52
  • This would seemingly prevent IME from working - at least that's the result that I see when I try this. – AndrewS Sep 30 '21 at 22:20