John Jared
John Jared

Reputation: 800

Android Matcher and Pattern cut up from the link

I have a string that contains a link.

ex. of the string:

We love to eat choco, eat one up at http://t.co/9BDZvcx59d.

So If I display this string as it is, it would be the same. But if I use matcher and pattern to detect the link and color it, it will cut it up.

It'll be: Bold is green color.

We love to eat choco, eat one up at http://t.co.

    Pattern urlPattern = Patterns.WEB_URL;

      Matcher m = urlPattern.matcher(sb.toString());
        sb = new StringBuffer(sb.length());

        while (m.find()) {
            m.appendReplacement(sb, "<font color=\"#006600\">" + m.group(1) + "</font>");
        }
        m.appendTail(sb);

I also tried using

    Pattern linkPattern = Pattern.compile("(http[A-Za-z0-9_-+)");

But couldn't insert : or // in the [].

Upvotes: 2

Views: 249

Answers (2)

Ahmed Ekri
Ahmed Ekri

Reputation: 4651

You can try this for your second method.

    Pattern linkPattern = Pattern.compile("(http://t.co/[A-Za-z0-9_-]+)");

It will compile after the t.co/ so it won't face symbols or anything but letter and numbers.

Upvotes: 1

Prashant Thakkar
Prashant Thakkar

Reputation: 1403

Try to add following attributes to your TextView in xml

android:autoLink="web"
android:textColorLink="your-color-code"

Upvotes: 1

Related Questions