1

I have a single TextView to display a large paragraph of text. This text may contain URLs, phone numbers, email addresses and other digits. I have to apply hyperlink only for URLs, phone numbers and email addresses. So used Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES | Linkify.WEB_URLS

But in android 8 (Oreo) year (eg : 2018) as hyper link. Is there any way to limit minimum number of digits to show as link? (There is no specified format for phone number in our application). Is there any way to set URLs, phone numbers and email addresses as links?

Asha Soman
  • 1,846
  • 1
  • 18
  • 28
  • https://stackoverflow.com/questions/7570239/android-linkify-text-spannable-text-in-single-text-view-as-like-twitter-twee. Custom Clickable Span? – Raghunandan Mar 22 '18 at 09:18
  • Its always better to use html text for these cases rather than spannable. – Rohit5k2 Mar 22 '18 at 09:21
  • @Rohit5k2 why is html option better than spans?. Spans give you lot of options – Raghunandan Mar 22 '18 at 09:23
  • Did you read "these cases"? It meant to show simple links. But html text is good for other stuffs too. – Rohit5k2 Mar 22 '18 at 09:26
  • I am not sure what you meant by "these cases." https://stackoverflow.com/questions/23552437/spannable-vs-typeface-vs-html. For a simple link yes you could use html. – Raghunandan Mar 22 '18 at 09:29

1 Answers1

-1

There doens't seem to be a digit count setting you could you to disable year turning into links.

There seems to be a way you could achieve this using regex. You can use below method

Linkify.addLinks(Spannable text, Pattern pattern, String scheme)

or

Linkify.addLinks(TextView text, Pattern pattern, String scheme)

Try the method that suits you best. Keep in mind first two parameters can't be null but you could pass scheme as null.

You will have to use three regex with | (or) representing email, phone number and urls.

See documentations for details

You can find regex samples here https://www.labnol.org/internet/regular-expressions-forms/28380/

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • We can't use pattern for this. Because the text may contain phone numbers with or without country code. That is why I have already specified that there is no specified format. Text may contain either url, email id, pone numbers, digits or all of these in same paragraph – Asha Soman Mar 22 '18 at 10:12
  • rather than using a regex for phone use a regex for at least 5 digit, ignoring +,- and (). That is what you were looking for, right? – Rohit5k2 Mar 22 '18 at 11:18
  • I don't want to show year as a link. is this a bug in Linkify interface? Do you know minimum length for digits to show as a link? If the digits length is atleast 6, then I have to show it as a link – Asha Soman Mar 22 '18 at 11:36
  • see from your question what i get is that you want to linkify email, phone and urls. No years such as 2018. A year would be maximum of 4 digits where as a phone number is at least 6 (that too without area code). So use a regex that matches any digit more than 4 or from 5to 10. – Rohit5k2 Mar 22 '18 at 11:39