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";
textview.setText(text);


"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);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}

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);
strBuilder.removeSpan(span);
}


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(
                tv.getText());
        InternalURLSpan span = new InternalURLSpan();
        span.setText(text.substring(x, y));
        span.setClickInterface(clickInterface);
        f.setSpan(span, x, y,
                android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(f);
    }
    tv.setLinksClickable(true);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    tv.setFocusable(false);
}

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

    private String text;
    private HandleLinkClickInsideTextView clickInterface;

    @Override
    public void onClick(View widget) {
        getClickInterface().onLinkClicked(getText());
    }

    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