0

In my app I have a webview that displays some content. One of these screen has a text box to fill in. I want to capture when the user presses the done button on the keyboard but there's no edit text to add a listener too. How can I capture the action regardless of device & keyboard?

I've tried these with little luck. EditText with textPassword inputType, but without Softkeyboard

android: Softkeyboard perform action when Done key is pressed

@Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:
                // code here
                break;
            default:
                return super.onKeyUp(keyCode, event);
        }
        return true;
    }

My class overrides KeyEvent.Callback but the above funtion onKeyDown is never called.

telaCode
  • 159
  • 11

2 Answers2

3

Create a custom webview, override onCreateInputConnection to set ime option and input type to keyboard, override dispatchKeyEvent to get the key events filter it out

Example :

class MyWeb@JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0) : WebView(context, attrs, defStyleAttr) {

override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
    val inputConnection = BaseInputConnection(this, false)
    return inputConnection
}

override fun dispatchKeyEvent(event: KeyEvent): Boolean {
    super.dispatchKeyEvent(event)
    val dispatchFirst = super.dispatchKeyEvent(event)
    if (event.action == KeyEvent.ACTION_UP) {
        when (event.keyCode) {
            KeyEvent.KEYCODE_ENTER -> {
                Toast.makeText(context,"Hii",Toast.LENGTH_LONG).show()
                //callback?.onEnter()
            }
        }
    }
    return dispatchFirst
}

}

and XML

<com.example.MyWeb
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:id="@+id/web"
/>`

Source : https://medium.com/@elye.project/managing-keyboard-on-webview-d2e89109d106

Ramees Thattarath
  • 1,093
  • 11
  • 19
1

Key events are almost never sent from a soft keyboard, they use more direct methods.

The way a keyboard on Android works is its bound to a view. That view has to implement getInputConnection() returning an object that will allow functions to be called (via AIDL) by the keyboard app. One of these functions is called for the "action key" (the done button). In the default InputConnection implementation, that will call a listener registered to the bound view.

Since you're dealing with a webview here- I don't think there is a way to capture it directly. What you can try is to subclass WebView to ActionKeyWebView. Add a function to register an action key listener interface. Override getInputConnection to return your own InputConnectionWrapper subclass, and wrap super.getInputConnection(). THen override performEditorAction to call any listener registered for the webview. Its a fair amount of code but it should work.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127