Kpan
Kpan copied to clipboard
LinkMovementMethod with text gravity and singleLine issue
private class LinkMovementMethodTouchListener : View.OnTouchListener {
override fun onTouch(
v: View,
event: MotionEvent
): Boolean {
val widget = v as TextView
val action = event.action
if (widget.text is SpannedString && action == MotionEvent.ACTION_UP) {
var x = event.x.toInt()
var y = event.y.toInt()
x -= widget.totalPaddingLeft
y -= widget.totalPaddingTop
x += widget.scrollX
y += widget.scrollY
val layout = widget.layout
val line = layout.getLineForVertical(y)
val off = layout.getOffsetForHorizontal(line, x.toFloat())
val links = (widget.text as SpannedString).getSpans(off, off, ClickableSpan::class.java)
if (links.isNotEmpty()) {
links[0].onClick(widget)
return true
}
}
return true
}
}