K Sathish K Sathish - 3 years ago 191
Android Question

Capture http link click event in android textview

I have a link in android textview. I am not able to capture the link click event.

String text = "http:://www.google.com is a google link";

"http:://www.google.com" this span of string is clickable in textview. I want to capture that particular click event.

I tried the following.

public static void setTextView(TextView text, CharSequence sequence) {
UoloLogger.i(TAG, "Setting string :: "+sequence);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span);

public static void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
UoloLogger.i(TAG, span.getURL());
strBuilder.setSpan(clickable, start, end, flags);

I started setting text into my textview using setTextView() method. I am getting URLSpan array is empty even if i am having the links.

String text = "http:://www.google.com is a google link";
setTextView(textView, text);

Sorry for the bad english. I think, i have explained my problem. Can someone help me.

Answer Source
public static void setLinkclickEvent(TextView tv, HandleLinkClickInsideTextView clickInterface) {
    String text = tv.getText().toString();
    String str = "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])";
    Pattern pattern = Pattern.compile(str);
    Matcher matcher = pattern.matcher(tv.getText());
    while (matcher.find()) {
        int x = matcher.start();
        int y = matcher.end();
        final android.text.SpannableString f = new android.text.SpannableString(
        InternalURLSpan span = new InternalURLSpan();
        span.setText(text.substring(x, y));
        f.setSpan(span, x, y,

public static class InternalURLSpan extends android.text.style.ClickableSpan {

    private String text;
    private HandleLinkClickInsideTextView clickInterface;

    public void onClick(View widget) {

    public void setText(String textString) {
        this.text = textString;

    public String getText() {
        return this.text;

    public void setClickInterface(HandleLinkClickInsideTextView clickInterface) {
        this.clickInterface = clickInterface;

    public HandleLinkClickInsideTextView getClickInterface() {
        return this.clickInterface;


public interface HandleLinkClickInsideTextView {
    public void onLinkClicked(String url);

After this i just did.

textview.setText("http://google.com is google website and http://youtube.com is youtube site");
setLinkclickEvent(textview, new HandleLinkClickInsideTextView() {
    public void onLinkClicked(String url) {
          // Here I added my code
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download