Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle clicks on TextView's links while using Linkify for finding and setting the links inside the text

i have a TextView filled with text which i get from a server. I'm using Linkify for handling all the link searching and for setting a URLSpan where needed throughout its addLinks method.

The problem is that the default behavior when clicking a link is opening it in a browser, what i want is to get the clicked link and handle it my self.

I don't see any method of Linkify which let me set a "OnClick" or something...

Thank for your help :)

like image 706
Shirane85 Avatar asked Nov 27 '13 12:11

Shirane85


People also ask

What is linkify textview in Android?

What is Linkify Textview in android? Before getting into the example we should know, what is linkify. Linkify is just like a Hyper link in HTML. Using that we can browse the content. Here is the simple solution to use linkify with textview in android.

What happens when you click on a link in a textview?

In both the cases, the framework internally registers a LinkMovementMethod on the TextView that handles dispatching a ACTION_VIEW Intent when any link is clicked. This is why phone-numbers open in a dialer when clicked, web URLs open in a browser, map URLs open in Google Maps and so on.

Can I use linkify auto without the a href?

if you use linkify auto you don't need the a href part. The OS will take the string parse it for urls and converts every url to a clickable link. But this won't result in the word google being linked to google.com. It would display www.google.com as link.

How to call linkify in Android?

To call linkify in android, we have to call linkify.addLinks (), in that method we have to pass textview and LinkifyMask. There are different type of LinkifyMask's are available as shown below - Linkify.WEB_URLS: It going make URL as web url, when user click on it, it going to send url to default web browsers.


1 Answers

Ok so i finally managed to set my own "OnClickListener" to the TextView's links. My solution was to copy Linkify to my project, name it CustomLinkify and just change its applyLink method:

From:

private static final void applyLink(String url, int start, int end, Spannable text) 
{
    URLSpan span = new URLSpan(url);

    text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

To:

private static final void applyLink(final String url, int start, int end, Spannable text) 
{
    URLSpan span = new URLSpan(url)
    {
        @Override
        public void onClick(View widget)
        {
            _onLinkClickListener.onLinkClicked(url);
        }
    };

    text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

Where _onLinkClickListener is a new field, set by me before using the new CustomLinkify.

I know its not a very elegant solution and i prefered google to allow setting a listener through the native Linkify, but, for me, this is better than implementing my own Spannable logics (as suggested in other related questions).

I trust the Linkify code and i guess i'll check from time to time to see if any changes made on it, if so, i'll of course update CustomLinkify with the changes.

Hope this will help someone.

like image 172
Shirane85 Avatar answered Sep 20 '22 02:09

Shirane85