add Links to TextView - Android User Interface

Android examples for User Interface:TextView

Description

add Links to TextView

Demo Code


import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.widget.TextView;
import android.text.method.LinkMovementMethod;

public class Main{
    public static void addLinks(TextView view) {
        CharSequence content = view.getText();
        view.setText(convertToSpannableString(content.toString()));
        if (view.getLinksClickable()) {
            view.setMovementMethod(LinkMovementMethod.getInstance());
        }// w w w  .  j  a v  a 2 s .c  o m
    }
    public static void addLinks(TextView view, String url) {
        CharSequence content = view.getText();
        view.setText(convertToSpannableString(url));
        if (view.getLinksClickable()) {
            view.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
    private static SpannableString convertToSpannableString(String txt) {
        String hackTxt;
        if (txt.startsWith("[") && txt.endsWith("]")) {
            hackTxt = txt + " ";
        } else {
            hackTxt = txt;
        }

        SpannableString value = SpannableString.valueOf(txt);
        Linkify.addLinks(value, ContentLinkPatterns.WEB_URL,
                ContentLinkPatterns.WEB_SCHEME, new Linkify.MatchFilter() {
                    @Override
                    public boolean acceptMatch(CharSequence charSequence,
                            int i, int i2) {
                        if (charSequence.toString().contains("@")) {
                            return false;
                        }
                        return true;
                    }
                }, null);

        URLSpan[] urlSpans = value.getSpans(0, value.length(),
                URLSpan.class);
        ContentURLSpan contentURLSpan = null;
        for (URLSpan urlSpan : urlSpans) {
            contentURLSpan = new ContentURLSpan(urlSpan.getURL());
            int start = value.getSpanStart(urlSpan);
            int end = value.getSpanEnd(urlSpan);
            value.removeSpan(urlSpan);
            value.setSpan(contentURLSpan, start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        return value;
    }
}

Related Tutorials