700 Software
700 Software

Reputation: 87763

Inline hyperlinks in Android TextView

How can I create inline hyperlinks in a TextView while having different values for the link URL and link text?

Upvotes: 1

Views: 1142

Answers (1)

jjb
jjb

Reputation: 3570

You can do this by using the SpannableString stuff that Android has. Specifically, you can attach a URLSpan to whatever text you want and set the URL to an arbitrary URL. Just create a SpannableString (or SpannableStringBuilder if you're composing several strings), attach a URLSpan using setSpan and then put that in the TextView. Note that you have to call setMovementMethod to an instance of LinkMovementMethod otherwise the clicks won't work. Oh, and don't try and have an onClick handler on the TextView as well, you're going to get very frustrated if you try and do that.

Upvotes: 2

Related Questions