Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:28:34+00:00 2026-05-27T23:28:34+00:00

I have a TextView . I have added custom links like @abc , #android

  • 0

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 the text of the link which is clicked. I am using SpannableString to setText to the textview. I then set spans using my custom ClickableSpan. It works fine. Plus I can also catch the onclick event. But the onClick() method has a View paramter. If I call getText() on the View (ofcourse after typecasting it to TextView), it returns the entire text.
I searched a lot but always found ways to add links and catch the event, but none told about getting the text of the link.

This is the code I am using to add links and recieve onclick. I got the code from one of the SO threads..

Pattern pattern = Pattern.compile("@[\\w]+");
Matcher matcher = pattern.matcher(tv.getText());//tv is my TextView
while (matcher.find()) {
    int x = matcher.start();
    int y = matcher.end();
    final android.text.SpannableString f = new android.text.SpannableString(
    tv.getText());
    f.setSpan(new InternalURLSpan(new View.OnClickListener() {
        public void onClick(View v) {
        showDialog(1);
    }
}), x, y, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(f);
tv.setLinkTextColor(Color.rgb(19, 111, 154));
tv.setLinksClickable(true);

Here is the InternalURLSpan:

class InternalURLSpan extends android.text.style.ClickableSpan {
    View.OnClickListener mListener;

    public InternalURLSpan(View.OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View widget) {
        mListener.onClick(widget);
        TextView tv = (TextView) widget;
        System.out.println("tv.gettext() :: " + tv.getText());
        Toast.makeText(MyActivity.this,tv.getText(),
        Toast.LENGTH_SHORT).show();
    }
}

Is it possible to get the text of the link clicked?
If not, is there a way of associating some data to a particular link and knowing which link gets clicked?
Any pointers.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:28:34+00:00Added an answer on May 27, 2026 at 11:28 pm

    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 –

    class InternalURLSpan extends android.text.style.ClickableSpan {
            public String text;
    
            @Override
            public void onClick(View widget) {
                handleLinkClicked(text);
            }
    
        }
    

    handleLinkClicked() is as –

    public void handleLinkClicked(String value) {
        if (value.startsWith("http")) {     // handle http links
        } else if (value.startsWith("@")) { // handle @links
        } else if (value.startsWith("#")) { // handle #links
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom Dialog that contains only a TextView to display some text
I have a gtk.Textview . I want to find and select some of the
I have the following TextView in my XML layout file:- <TextView android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/autolink_test
I have created an App Widget for Android 1.5. It uses a TextView to
Hi I'd like to programatically increase the height allocated to a TextView, and have
I have created one gridview and use custom adapter for that. I added two
I have added two textview in iphone by this way. I have two UITextView
previously i have added a ontouch listener to textviews which will do a custom
I have used a customized list view in which i have added 4 TextView
i have have added rows in tables... but after adding some rows table have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.