Using Linkify.addLinks combine with Html.fromHtml

VimToe

New Member
I have a \[code\]TextView\[/code\] that gets it's data set by calling this:\[code\]tv.setText(Html.fromHtml(myText));\[/code\]The string \[code\]myText\[/code\] contains partially formatted html data. For example, it might have font tags, but not have any url links formatted using \[code\]<a href=http://stackoverflow.com/questions/14538113/...>\[/code\] tags. I was hoping to use the \[code\]Linkify.addLinks(...)\[/code\] 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:\[code\]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());\[/code\]This does not work properly. Meaning that it processes the font tags but Linkify does not convert urls to \[code\]UrlSpan\[/code\] 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)\[code\]<TextView ... android:autoLink="all" android:linksClickable="true" .../>\[/code\]Can someone explain to me why that would make everything work??I looked into the source code for TextView's \[code\]setMovementMethod()\[/code\] and it eventually ends up calling:\[code\]setFocusable(true);setClickable(true);setLongClickable(true);\[/code\]That should theoretically make everything work and behave the same as the xml layout code. I tried switching the order of calling \[code\]Linkify.addLinks(..)\[/code\] before \[code\]setText(Html.fromHtml(..))\[/code\] 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?
 
Back
Top