4

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.

K Sathish
  • 247
  • 3
  • 13
  • Follow the steps given here by Arunkumar https://stackoverflow.com/questions/7722806/get-the-value-of-link-text-when-clicked-in-a-textview-in-android – Harminder Singh Sep 21 '17 at 12:14
  • 1
    Follow this: https://stackoverflow.com/questions/7722806/get-the-value-of-link-text-when-clicked-in-a-textview-in-android – Harminder Singh Sep 21 '17 at 12:16
  • Does this answer your question? [Android TextView with Clickable Links: how to capture clicks?](https://stackoverflow.com/questions/12418279/android-textview-with-clickable-links-how-to-capture-clicks) – Top-Master May 24 '22 at 21:06

3 Answers3

4
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 used the method send click event.

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
    }
});
K Sathish
  • 247
  • 3
  • 13
0

If you want to open a link after textview click, there are two options:

  1. Using java code:

    Spanned text = Html.fromHtml("<u>GOOGLE.COM</u>");
    textView.setText(text);
    Uri uri = Uri.parse("http://shopwhere.com.au/");
    Intent webIntent = new Intent(Intent.ACTION_VIEW,uri);
    // Create and start the chooser
    Intent chooser = Intent.createChooser(webIntent, "Open with");
    startActivityForResult(chooser,0);
    
  2. Using XML:

    Use android:autoLink="web" inside textview tag. You can also change link color android:textColorHighlight="@android:color/transparent" and android:textColorLink="@color/white".

tuomastik
  • 4,559
  • 5
  • 36
  • 48
  • 1
    I can open the link. I want to capture which link has clicked and i want to execute some piece of code. – K Sathish Sep 21 '17 at 12:07
0

You can achieved the same using SpannableStringBuilder.

Simply initialize the TextView that you want to add 2 or more listeners and then pass that to the following method that I have created:

SAMPLE CODE:

private void customTextView(TextView view) {
        SpannableStringBuilder spanTxt = new SpannableStringBuilder(
                "I agree to the ");
        spanTxt.append("Term of services");
        spanTxt.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "Terms of services Clicked",
                        Toast.LENGTH_SHORT).show();
            }
        }, spanTxt.length() - "Term of services".length(), spanTxt.length(), 0);
        spanTxt.append(" and");
        spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 32, spanTxt.length(), 0);
        spanTxt.append(" Privacy Policy");
        spanTxt.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "Privacy Policy Clicked",
                        Toast.LENGTH_SHORT).show();
            }
        }, spanTxt.length() - " Privacy Policy".length(), spanTxt.length(), 0);
        view.setMovementMethod(LinkMovementMethod.getInstance());
        view.setText(spanTxt, BufferType.SPANNABLE);
    } 

And in your XML, use android:textColorLink to add custom link color of your choice. Like this:

  <TextView
     android:id="@+id/textView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="TextView"
     android:textColorLink="#C36241" /> 
Saneesh
  • 1,796
  • 16
  • 23