0

I can't figure out how to use Linkify for my TextView. I'm currently trying:

Linkify.addLinks(myTextView, Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"), "http://");

I am using this post for my regex, which checks out here.

Am I missing something that I have to do in order to have URLs in my TextView clickable?

Essentially, I am hoping to have my TextView show my URLs as clickable links and actually click through, just like iOS's UITextViews are able to by changing the dataDetectorTypes property to match the proper UIDataDetectorType.

Community
  • 1
  • 1
RileyE
  • 10,874
  • 13
  • 63
  • 106
  • try the other method from [here](http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable) – vikki Mar 22 '13 at 22:09
  • @vikki Thank you, however, I've already tried that one as well. – RileyE Mar 22 '13 at 22:10

2 Answers2

3

Use android:autoLink="web" in your layout XML for the TextView to handle http/https.

If you wish to continue using Linkify, be sure to do this after you have set the text in the TextView, not before.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I've tried setting it in Java using `myTextField.setAutoLinkMask(Linkify.WEB_URLS);`, however, that doesn't effect it at all. Is the XML different? My `TextView` isn't in XML anywhere. – RileyE Mar 22 '13 at 22:06
  • @RileyE: "Is the XML different?" -- yes. It's fewer characters. :-) Beyond that, though, I would have expected your code to have worked. – CommonsWare Mar 22 '13 at 22:21
  • I would have figured that, as well. Thank you so much for the suggestion, though! – RileyE Mar 22 '13 at 22:29
2

If you want to use Linkfy there should be no need to write an own regex pattern for web urls, just use

 Linkify.addLinks(text, Linkify.WEB_URLS);
fish
  • 828
  • 1
  • 6
  • 14