I have a TextView that gets its dataset, calling this:
tv.setText(Html.fromHtml(myText));
The myText line contains partially formatted html data. For example, it may have font tags, but it does not have URLs formatted using <a href=...> tags. I was hoping to use Linkify.addLinks(...) to do this, as my text may include other types of links that Linkify will convert for me accordingly. So I wrote my code 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. This means that it handles font tags, but Linkify does not correctly convert URLs to UrlSpan .
Alternatively, if I call setText () without Html.fromHtml (..), Linkify works, but then I lose all the text formatted from the html font tags. Somehow they both seem contradictory, and I can only have one or the other.
Now here is the interesting part that I do not understand. If I remove the Linkify code from java and go to the xml layout and put the following lines there, everything seems to work (Linkify and fromHtml both end up playing well together ... somehow)
<TextView ... android:autoLink="all" android:linksClickable="true" ... />
Can someone explain to me why this would make everything work?
I looked at the source code for the TextView setMovementMethod() , and in the end it calls:
setFocusable(true); setClickable(true); setLongClickable(true);
This should theoretically make everything work and behave the same as the xml layout code. I tried switching the calling order of Linkify.addLinks(..) to setText(Html.fromHtml(..)) in the java code, but that did not affect.
Any ideas as to why combining Linkify.addLinks () and Html.fromHtml () in java causes this behavior ... but not in the xml layout?