Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linkify.addLinks combine with Html.fromHtml

I have a TextView that gets it's data set by calling this:

tv.setText(Html.fromHtml(myText));

The string myText contains partially formatted html data. For example, it might have font tags, but not have any url links formatted using <a href=...> tags. I was hoping to use the Linkify.addLinks(...) to do that since my text could include other types of links that Linkify would convert for me appropriately. So I wrote my code to look like this:

String myText = "<font color=\"red\">Red text</font> and Url: www.google.com";
tv.setText(Html.fromHtml(myText));
Linkify.addLinks(tv, Linkify.ALL);
tv.setMovementMethod(LinkMovementMethod.getInstance());

This does not work properly. Meaning that it processes the font tags but Linkify does not convert urls to UrlSpan properly.

Alternatively, if I call setText() without the Html.fromHtml(..), Linkify works but then I lose all the text formatted from the html font tags. Somehow they both seem to be conflicting and I can have only one or the other.

Now here's the interesting part that I dont understand. If I remove the Linkify code from java and go to my layout xml and put the following lines in there, all seems to be working (Linkify and fromHtml both end up playing nice together... somehow)

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

Can someone explain to me why that would make everything work??

I looked into the source code for TextView's setMovementMethod() and it eventually ends up calling:

setFocusable(true);
setClickable(true);
setLongClickable(true);

That should theoretically make everything work and behave the same as the xml layout code. I tried switching the order of calling Linkify.addLinks(..) before setText(Html.fromHtml(..)) in the java code, but that didn't make a difference.

Any ideas as to why combining Linkify.addLinks() and Html.fromHtml() in java would cause this behavior... but not in the xml layout?

like image 262
wnafee Avatar asked Jan 26 '13 14:01

wnafee


2 Answers

You can try this one:

First set the text in to your TextView.

tv.setText(myText);

Convert the links with Linkify

Linkify.addLinks(tv, Linkify.ALL);

and finally replace the text with Html.fromHtml but using the Linkified text from your EditText.

tv.setText(Html.fromHtml(tv.getText().toString()));
like image 163
josealfonsomora Avatar answered Nov 04 '22 02:11

josealfonsomora


It's because Html.fromHtml and Linkify.addLinks removes previous spans before processing the text.

Use this code to get it work:

public static Spannable linkifyHtml(String html, int linkifyMask) {
    Spanned text = Html.fromHtml(html);
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);

    SpannableString buffer = new SpannableString(text);
    Linkify.addLinks(buffer, linkifyMask);

    for (URLSpan span : currentSpans) {
        int end = text.getSpanEnd(span);
        int start = text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    return buffer;
}
like image 36
gmazzo Avatar answered Nov 04 '22 03:11

gmazzo