First, you need to listen for View.OnLongClickListener. You can do that very easily by setting a listener on the WebView. Listener has to return a Boolean, return false if you want the copy, cut and paste on text selection or return true
webView.setOnLongClickListener { view ->
// run things on long click on any element in the webview
return@setOnLongClickListener false
}
Then create a Handler
val handler = Handler { message ->
val bundle = message.data
for (key in bundle.keySet()) {
Log.d(TAG, "KEY: $key, VALUE: ${bundle.get(key)}")
}
val linkText = bundle.get("title") // here is your link text
return@Handler true
}
Now you need to obtain the message (this code has to go inside the OnLongClickListener)
val message = handler.obtainMessage()
webView.requestFocusNodeHref(message)
Copying text to the ClipBoard got a little bit complicated.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
val clipData = android.content.ClipData.newPlainText("text label", "text to clip")
clipboardManager.primaryClip = clipData
} else {
val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as android.text.ClipboardManager
clipboardManager.text = "text to clip"
}