Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView that is linkified and selectable?

I would like to have a TextView that is both selectable and linkified. When I do both I end up with selectable text but links can't be clicked.

EDIT:

I'll show the code to explain with what I struggle:

    TextView textView = view.findViewById(R.id.mytext);
    textView.setText("My text: +4412345678 Go to website: www.google.com Blah blah");
    Linkify.addLinks(textView, Linkify.ALL);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        textView.setTextIsSelectable(true);
    }
like image 713
pixel Avatar asked Feb 13 '13 20:02

pixel


3 Answers

Did you try to add this on your TextView xml code?

    <TextView
    ...
    android:autoLink="all"
    android:textIsSelectable="true" />

I've tried it on my code and i can make a call/surf on web/mail and also select all text.

like image 103
JJ86 Avatar answered Oct 14 '22 11:10

JJ86


The autoLink attribute has an annoying bug: if you click in your example on the phone number, then return back and click on the second url link - it will open the phone number again. This attribute works so bad with multiple links, that I have implemented my own class, here is the link on Github ClickableLinksTextView.java

In your example you can replace your TextView class by my ClickableLinksTextView class in the xml-layout and change the code like this:

ClickableLinksTextView textView = (ClickableLinksTextView)view.findViewById(R.id.mytext);
textView.setText("My text: +4412345678 Go to website: www.google.com Blah blah");
Linkify.addLinks(textView, Linkify.ALL);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    textView.setMovementMethod(ArrowKeyMovementMethod.getInstance());
    textView.setTextIsSelectable(true);
    // the autoLink attribute must be removed, if you hasn't set it then ok, otherwise call textView.setAutoLink(0);
}

The original cause of the issue in your question is that the LinkMovementMethod class and the textIsSelectable attribute are not compatible at all, even Android OS developers admit this in the Android OS source code.

Do not change the movement method for text that support text selection as it would prevent an arbitrary cursor displacement.

If the text view is selectable, the only 2 correct movement method values are null and ArrowKeyMovementMethod. That's why I set ArrowKeyMovementMethod explicitly in my example, because the Linkify.addLinks sets an incorrect movement method and I should revert it.

As to the bug of the autoLink attribute, it is because android developers haven't copied link detection properly. You can look at the code example in the answer of @cheng yang, the code just takes the first link no matter how many of them you have.

like image 37
vortexwolf Avatar answered Oct 14 '22 11:10

vortexwolf


The problem is in Android's TextView. Calling Linkify.addLinks() would not change autolinkmask in TextView itself. I think it's an Android bug that they check mAutoLinkMask. So if you set android:autoLink in xml file, or call setAutoLinkMask() to a non 0 value, it will work.

FYI, TextView's source code:

            if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(),
                    getSelectionEnd(), ClickableSpan.class);

            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }
like image 2
cheng yang Avatar answered Oct 14 '22 12:10

cheng yang