I am a bit confused about the ‘rules’ of when a TextView element displays text in formatted form or not.
A string like
"There are <i>different ways</i> of coding.\n";
displays without any formatting (including the HTML codes) when I code
tvMyTextView.setText("There are <i>different ways</i> of coding.\n");
but when I define the same string in strings.xml and then load
tvMyTextView.setText(R.strings.TestString);
it displays emphasized.
Even more confused I feel when trying to embed URLs in TextView’s like here:
"Click <a href="http://www.poon-world.com">here</a> to switch on the red light.\n";
Needless to say I already tried the various property options of TextView – but they don’t seem to make much of a difference unless I missed something. In some cases the URL is encoded in the text, in blue color and can be clicked, in others I can see the HTML formatting. In others again, it is color-encoded and the URL seems to be encoded in the text somehow – but nothing happens when I click it. Regarding the embedding of URLs, unlike for the other example with ‘simple’ HTML formatting, I couldn’t even find out a rule so far of when it works and when it doesn’t. Can anyone help me to untie the knots in my head..
Actually, From the Android Docs..
Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.
But,
But from Android Resource String docs..
You can add styling to your strings with HTML markup. For example:
Supported HTML elements include:
Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won’t work because the
String.format(String, Object...)method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered withfromHtml(String), after the formatting takes place.And about your URL string,…
Also look at this SO Question Set TextView text from html-formatted string resource in XML
and
Android String Resource