saket / Better-Link-Movement-Method

Attempts to improve how clickable links are detected, highlighted and handled in TextView
Apache License 2.0
780 stars 78 forks source link

Differentiating between phone number / url #34

Closed TC-jZhang closed 4 years ago

TC-jZhang commented 4 years ago

I want to create functionality where I can linkify all of a textView, but only handle cases where it is a phone number on my own. For URL's, emails, etc. I want it to default to Linkify's default behavior:

 BetterLinkMovementMethod.linkify(Linkify.ALL, thistextView
     .setOnLinkClickListener((textView, url) -> {
    // Is there a way here to check for phone numbers without having to check for regex?
      if (url.matches.(phoneNumberRegex)) {
       //handle phone number click ourselves 
       return true
        } else {
         // we want to default to the default action intent of opening a URL/email and everything else EXCEPT phone number
          return false;
        }
     })

right now we are doing this:

LinkifyCompat.addLinks(textView, Linkify.ALL);
BetterLinkMovementMethod.linkify(Linkify.PHONE_NUMBERS, textView).setOnLinkClickListener(onLinkClickListener);

but you can see that it will only link phone numbers at this point. This is not an issue with the library, but asking more for suggestions or if there's any available API to use? Thanks!!

saket commented 4 years ago

You're overriding all links with only phone number links by using BetterLinkMovementMethod.linkify(PHONE_NUMBERS). I think you just need,

LinkifyCompat.addLinks(textView, Linkify.ALL)
textView.movementMethod = BetterLinkMovementMethod.newInstance().setOnLinkClickListener(onLinkClickListener)

Every time I see this I am reminded that adding linkify* functions to BetterLinkMovementMethod was a mistake 😄.

TC-jZhang commented 4 years ago

right, but how would I go about ONLY handling phone numbers in this case? Do I have to specifically look for a number regex?

saket commented 4 years ago

Yep, you'll have to run a regex check. I believe you can find the regex inside Linkify.

TC-jZhang commented 4 years ago

Sounds good. Thank you for your help!!

saket commented 4 years ago

no problem!

mahbub-java commented 3 years ago

Patterns Class has a solution...

if (Patterns.WEB_URL.matcher(url).matches()) {
    //An web url is detected 
    return true;
}
else if(Patterns.PHONE.matcher(url).matches()){
    //A phone number is detected 
    return true;
}
else if(Patterns.EMAIL_ADDRESS.matcher(url).matches()){
    //An email address is detected 
    return true;
}