Search code examples
androidphone-numberlinkify

Android Linkify Phone Numbers - how to specify which numbers to link and which not to


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.


Solution

  • 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'