Top Menu

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday 18 September 2014

How to write text in bold, normal, small format and in next line


What if we would like to read the the Html text from string.xml resource and thus make it easy to localize. CDATA make this possible:

<string name="my_text">

  <![CDATA[

    <b>Autor:</b> Mr Nice Guy<br/>

    <b>Contact:</b> myemail@grail.com<br/>

    <i>Copyright © 2011-2012 Intergalactic Spacebar Confederation </i>

  ]]>

</string>

From our Java code we could now utilize it like this:

TextView tv = (TextView) findViewById(R.id.myTextView);

tv.setText(Html.fromHtml(getString(R.string.my_text)));

Hope it's useful to some of you!

If you use many HTML tags a CDATA section is better. For the sample above it looks like this:

<string name="htmlFormattedText">
      <![CDATA[

 

      <p>Text with markup for <strong>bold</strong>
      and <em>italic</em> text.</p>

 

      <p>There is also support for a
      <tt>teletype-style</tt> font.

 

      But no use for the <code>code</code>
      tag!</p>

 

      ]]></string>

Even if you can add a lot of HTML tags, you are better off using only minor styling as mixing too much styles makes your text look uneasy instead of being more striking.

The following snippet shows how to use this string from within your Java code:

TextView view = (TextView)findViewById(R.id.sampleText);
String formattedText = getString(R.string.htmlFormattedText);

 

Spanned result = Html.fromHtml(formattedText);
view.setText(result);

 

Link for reference:


No comments:

Post a Comment