HTML formatting for TextView

I'm a little confused about the β€œrules” when a TextView element displays text in a formatted form or not.

Type string

"There are <i>different ways</i> of coding.\n";

displays without any formatting (including HTML codes) when I code

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

but when I define the same line in strings.xml and then load

tvMyTextView.setText(R.strings.TestString);

displayed underlined.

I feel even more embarrassed when I try to embed URLs in a TextView, like here:

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

, TextView - , , , . URL- , HTML. , URL- - , , . URL-, "" HTML, , , . - .

+5
3

, Android Docs..

public final void setText (CharSequence text)

TextView. TextView HTML-, XML. , android.text.style. * SpannableString XML.

,

public final void setText (int resid)
  • .

Android Resource String docs..

HTML. :

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

HTML:

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

, . , String.format(String, Object...) . HTML , fromHtml(String) .

URL,...

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

SO- TextView html- XML

Android String Resource

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

-.

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

+3

, , , , , .

- , URL (, http://www.poon-world.com, "Poon-World.com", , ), TextView , . , HTML , - , , , Html.fromHtml(..).

, : / "" URL- Click, , Html.fromHtml.. !, , OnCreate-Event :

myTextView.setMovementMethod(LinkMovementMethod.getInstance());

( , ). , , , Android. , , , , , - , , , .

0

All Articles