8

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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Seriously: thank you just about a million times 🙏🏻. What did you mean by "fixes some issues but introduces others, YMMV."?
@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.
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.
Tried this and it wasn't working initially. Until I found this: medium.com/mobile-app-development-publication/… 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.
This would seemingly prevent IME from working - at least that's the result that I see when I try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.