Similar to this SO question, I'm trying to display clickable links in a TextView: How do I make links in a TextView clickable?
But I don't just want the link to open a website. I want to handle the click event myself and, say, launch an activity. Here is what I have so far:
myTextView.movementMethod = object: LinkMovementMethod() {
override fun onTouchEvent(widget: TextView?, buffer: Spannable?, event: MotionEvent?): Boolean {
doTheThing()
return super.onTouchEvent(widget, buffer, event)
}
}
And on my TextView:
android:text="Foo <a href="bar">Bar</a>"
As expected, "Bar" is underlined in blue, but the onTouchEvent is tripped whenever I click on the whole TextView, including on Foo. I only want it to fire when I tap "Bar" because that's what's underlined. Also, what if I had multiple links in the text? How would I get each one to respond differently?