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.
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