Skip to content Skip to sidebar Skip to footer

Get The Value Of Link Text When Clicked In A Textview In Android

I have a TextView. I have added custom links like '@abc', '#android' by matching some regex pattern. The links are displaying properly. However I am not getting a way to extract th

Solution 1:

The solution goes like this -

Call setLinks() with you textview and the text to be added.

setLinks(textView, text);

setLinks() function is as -

void setLinks(TextView tv, String text) {
        String[] linkPatterns = {
                "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])",
                "#[\\w]+", "@[\\w]+" };
        for (String str : linkPatterns) {
            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.text = text.substring(x, y);
                f.setSpan(span, x, y,
                        android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(f);
                // tv.setOnLongClickListener(span.l);

            }
        }
        tv.setLinkTextColor(Color.BLUE);
        tv.setLinksClickable(true);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setFocusable(false);
    }

and the InternalURLSpan class goes like this -

classInternalURLSpanextendsandroid.text.style.ClickableSpan {
        publicString text;

        @OverridepublicvoidonClick(View widget) {
            handleLinkClicked(text);
        }

    }

handleLinkClicked() is as -

publicvoidhandleLinkClicked(String value) {
    if (value.startsWith("http")) {     // handle http links
    } elseif (value.startsWith("@")) { // handle @links
    } elseif (value.startsWith("#")) { // handle #links
    }
}

Solution 2:

Here is a pretty simple solution I found to get the value of the link inside the TextView when the user clicks on it. In this case I'm using phone numbers and it works like a charm.

myTextView.setOnClickListener(newView.OnClickListener() {

        @OverridepublicvoidonClick(View v) {
            if(myTextView.getSelectionStart()== -1 && 
                    myTextView.getSelectionEnd() == -1){
                Toast.makeText(getApplicationContext(), "You clicked outside the link", 
                        Toast.LENGTH_SHORT).show();
            }
            else {

                intstart= myTextView.getSelectionStart();
                intend= myTextView.getSelectionEnd();
                Stringselected= myTextView.getText().toString().substring(start, end);

                Toast.makeText(getApplicationContext(), 
                        "Clicked: " + selected, 
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

Hope it helps.

Solution 3:

Use

android:linksClickable="true" android:autoLink="web"

textView.setMovementMethod(LinkMovementMethod.getInstance())

Post a Comment for "Get The Value Of Link Text When Clicked In A Textview In Android"