0

I have a big paragraph which may have numbers, email addresses and links. So I have to set setAutoLinkMask(Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES | Linkify.WEB_URLS) for my textview.

The content may contain digits of varying numbers. I want to set numbers having atleast 8 digits as phone number links.(For Eg : 12345678)

Is it possible to set minimum length for Linkify.PHONE_NUMBERS ? Is there anyway to achieve this?

Asha Soman
  • 1,846
  • 1
  • 18
  • 28

3 Answers3

0

In case you can use Linkify.MatchFilter to specify minimum length or your some other requirements. There is not any direct way provided by Android.

Also somewhere in this SO post found some good examples.

Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
0

use below pattern :

    SpannableString buffer = new SpannableString(text);
    Pattern pattern = Pattern.compile("^[0-9]\d{7,9}$");
    Linkify.addLinks(buffer , pattern,"");
Gautam Kushwaha
  • 281
  • 3
  • 15
0

Yes, its possible. I researched this phenomenon :-)

To set the minimum length for a phone number, use this code:

private final Linkify.MatchFilter matchFilterForPhone = new Linkify.MatchFilter() {
    @Override
    public boolean acceptMatch(CharSequence s, int start, int end) {
        int digitCount = 0;
        for (int i = start; i < end; i++) {
            if (Character.isDigit(s.charAt(i))) {
                digitCount++;
                if (digitCount >= 6) { // HERE: number 6 is minimum
                    return true;
                }
            }
        }
        return false;
    }
};

To properly format and link phone numbers, use:

final SpannableString s = new SpannableString(myTekst);
Linkify.addLinks(s, android.util.Patterns.PHONE, "tel:", matchFilterForPhone, Linkify.sPhoneNumberTransformFilter);

Now place the formatted s in your TextView, and call:

findViewById(R.id.message).setLinkTextColor(Color.BLUE);
findViewById(R.id.message).setMovementMethod(LinkMovementMethod.getInstance());

That's all. Thanks for vote.