Android Matcher and template cut from link

I have a line containing a link.

ex. line:

We love eating choco, eat it at http://t.co/9BDZvcx59d .

So, if I show this line as is, it will be the same. But if I use a match and pattern to detect the link and color it, it will shorten it.

It will be: Bold is green.

We love eating choco, eat alone 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 failed to insert: or // into [].

+2
source share
2 answers

You can try this for your second method.

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

t.co/, - , .

+1

TextView xml

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

All Articles