1

I'm writing an Android app in Java which displays information relating to Spain, including phone numbers.

I am using Linkify to make these phone numbers linkable when they're displayed using this code

Linkify.addLinks(textViewPlaceNotesDetailView, Linkify.PHONE_NUMBERS);

This works fine.

My problem is that other numbers are also appearing as links, ie. years like 1965.

How can I prevent this? I only want phone numbers to be links.

NB. I thought of specifying a minimum length for the number, in this case 9 (Spanish phone numbers are 9 digit in the format 'xxx xxx xxx'). But I haven't been able to figure out how to do this.

  • Tried this? https://stackoverflow.com/questions/27927930/android-linkify-clickable-telephone-numbers – sajjad Oct 10 '20 at 12:15
  • @Sajjad Thanks for your suggestion, I saw that question but it doesn't help me. My links work for phone numbers, I want to prevent them working for other numbers. The information in that question also seems be outdated. – Gearóid Ó Ceallaigh Oct 10 '20 at 12:21

2 Answers2

1

Well you can use a custom regex to linkify the phone number.

 tv.setLinksClickable(true);
 //only find phone number
 //Pattern phoneNumberPattern = Pattern.compile("\\d{3}\\s\\d{3}\\s\\d{3}");
 //find phone number or web url
 Pattern phoneNumberPattern = Pattern.compile("(\\d{3}\\s\\d{3}\\s\\d{3})|((https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])");
 Linkify.addLinks(tv, phoneNumberPattern,"");

The provided regex is detecting both xxx xxx xxx number format and web urls which is working for your case.

sajjad
  • 834
  • 6
  • 13
  • Thanks @Sajjad, I have implemented the following but my phone numbers are now not links: `textViewPlaceNotesDetailView.setLinksClickable(true); Pattern phoneNumberPattern = Pattern.compile("\\s?(?:6[0-9]|7[1-9]|8[1-9]|9[1-9])[0-9]\\s?[0-9]{3}\\s?[0-9]{3}$"); Linkify.addLinks(textViewPlaceNotesDetailView, phoneNumberPattern,"");`; Do I need some specific XML formatting in the TextView_ – Gearóid Ó Ceallaigh Oct 10 '20 at 15:44
  • Well, obviously you have to use the proper regex. I updated my answer and used another regex. Try it! @GearóidóCeallaigh – sajjad Oct 11 '20 at 06:17
  • Great, the above works perfectly for phone numbers and years. However if I add `Linkify.addLinks(textViewPlaceNotesDetailView, Linkify.WEB_URLS);` after it to linkify web urls then my phone numbers are no longer links. How can I combine the two? – Gearóid Ó Ceallaigh Oct 11 '20 at 09:35
  • Added the regex to find both phone numbers and web Urls. You could find it yourself with a little bit of search. @GearóidóCeallaigh – sajjad Oct 11 '20 at 10:56
  • Edit your question title and add "Web urls" to the title and body of your question. – sajjad Oct 11 '20 at 10:57
  • I would prefer to leave the original question as is and revert to your answer which resolved it. If necessary I can raise another question for the other issue, since it seems to me to be something different. Your solution only partially works for me because it requires http:// before the URL, which I don't have in my dataset. – Gearóid Ó Ceallaigh Oct 11 '20 at 11:31
  • Try to edit the `regex` to fit your needs. What is exactly your `url` format? Do they end in specific domain like .com or .org? if so then you can simply find the proper `regex` for that. – sajjad Oct 11 '20 at 11:51
0

Changing to using LinkifyCompat seems to have fixed this issue.

The code I'm now using is:

LinkifyCompat.addLinks(textViewPlaceNotesDetailView, Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);

This works for both (Spanish) phone numbers and URLs.

I'm pretty certain this didn't resolve the issue when I first tried it but since then I have updated to use the latest dependencies and it is now working.

I don't know which library deals with this, but my dependencies now looks like this:

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'org.osmdroid:osmdroid-android:6.1.7'
implementation 'org.osmdroid:osmdroid-shape:6.1.6'
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.navigation:navigation-fragment:2.3.4'
implementation 'androidx.navigation:navigation-ui:2.3.4'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation files('libs/commons-net-3.3.jar')
implementation 'com.android.support:design'