Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML formatting for TextView

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..

like image 778
richey Avatar asked Jun 16 '12 10:06

richey


2 Answers

Actually, From the Android Docs..

public final void setText (CharSequence text)

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,

public final void setText (int resid)
  • no more specification on it..

But from Android Resource String docs..

You can add styling to your strings with HTML markup. For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="welcome">Welcome to <b>Android</b>!</string>
</resources>

Supported HTML elements include:

<b> for bold text.
<i> for italic text.
<u> for underline text.

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 with fromHtml(String), after the formatting takes place.

And about your URL string,...

tvMyTextView.setText(Html.fromHtml("Click <a href="http://www.poon-world.com">here</a> to switch on the red light.\n"));

Also look at this SO Question Set TextView text from html-formatted string resource in XML

and

Android String Resource

like image 125
user370305 Avatar answered Sep 28 '22 07:09

user370305


tvMyTextView.setText(Html.fromHtml("There are <i>different ways</i> of coding.\n"));

also try

Below link For linkify so automatically website link assign.

http://developer.android.com/resources/articles/wikinotes-linkify.html

like image 40
Nikhil Avatar answered Sep 28 '22 08:09

Nikhil