Skip to content Skip to sidebar Skip to footer

Setting Minimum Length For Auto Link

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

Solution 1:

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.

Solution 2:

use below pattern :

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

Solution 3:

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

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

privatefinal Linkify.MatchFiltermatchFilterForPhone=newLinkify.MatchFilter() {
    @OverridepublicbooleanacceptMatch(CharSequence s, int start, int end) {
        intdigitCount=0;
        for (inti= start; i < end; i++) {
            if (Character.isDigit(s.charAt(i))) {
                digitCount++;
                if (digitCount >= 6) { // HERE: number 6 is minimumreturntrue;
                }
            }
        }
        returnfalse;
    }
};

To properly format and link phone numbers, use:

finalSpannableStrings=newSpannableString(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.

Post a Comment for "Setting Minimum Length For Auto Link"