I have a standard LinkMovementMethod
established in my TextView
to push a web Activity of some sort when the user touches a link. However, I want to establish a "do you want to see the link" dialog rather than taking the user straight to the webpage. I've tried overriding the touch methods but it all gets a little convoluted. A little help?
Asked
Active
Viewed 1,864 times
3

QED
- 9,803
- 7
- 50
- 87
1 Answers
5
You can accomplish it in two ways:
- Create custom Spans: more complicated, but you can accomplish more customised text consisting of clickable parts (or bold, differently coloured etc). To know more, check out ClickableSpan and SpannableStringBuilder
- Extend
LinkMovementMethod
to accept custom click listener
In my opinion second solution is better in basic cases like yours. Here is how you can do it:
- Copy this java class: InternalLinkMovementMethod to your project
- Add set the link movement method of your TextView to this custom one, providing a click listener:
OnLinkClickedListener clickListener = new OnLinkClickedListener() {
@Override
public boolean onLinkClicked(String linkText) {
// here you can handle your click, eg show the dialog
// `linkText` is the text being clicked (the link)
// return true if handled, false otherwise
}
}
yourTextView.setMovementMethod(new InternalLinkMovementMethod(clickListener));

Rafal Zawadzki
- 963
- 6
- 15
-
Your `linkText` is the URL? – QED May 15 '18 at 12:19
-
Yes, its the text being clicked. Updated my answer – Rafal Zawadzki May 16 '18 at 02:56
-
1I don't remember what I was after here but the project is long since completed. I'll accept and move on. – QED Jan 21 '19 at 19:42