Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5954123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:55:33+00:00 2026-05-22T17:55:33+00:00

I have some fixed strings inside my strings.xml , something like: <resources> <string name=somestring>

  • 0

I have some fixed strings inside my strings.xml, something like:

<resources>
    <string name="somestring">
        <B>Title</B><BR/>
        Content
    </string>
</resources>

and in my layout I’ve got a TextView which I’d like to fill with the html-formatted string.

<TextView android:id="@+id/formattedtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/htmlstring"/>

if I do this, the content of formattedtext is just the content of somestring stripped of any html tags and thus unformatted.

I know that it is possible to set the formatted text programmatically with

.setText(Html.fromHtml(somestring));

because I use this in other parts of my program where it is working as expected.

To call this function I need an Activity, but at the moment my layout is just a simple more or less static view in plain XML and I’d prefer to leave it that way, to save me from the overhead of creating an Activity just to set some text.

Am I overlooking something obvious? Is it not possible at all? Any help or workarounds welcome!

Edit: Just tried some things and it seems that HTML formatting in xml has some restraints:

  • tags must be written lowercase

  • some tags which are mentioned here do not work, e.g. <br/> (it’s possible to use \n instead)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:55:34+00:00Added an answer on May 22, 2026 at 5:55 pm

    Just in case anybody finds this, there’s a nicer alternative that’s not documented (I tripped over it after searching for hours, and finally found it in the bug list for the Android SDK itself). You CAN include raw HTML in strings.xml, as long as you wrap it in

    <![CDATA[ ...raw html... ]]>
    

    Edge Cases:

    • Characters like apostrophe (‘), double-quote ("), and ampersand (&) only need to be escaped if you want them to appear in the rendered text AS themselves, but they COULD be plausibly interpreted as HTML.
      • ' and " can be represented as\' and \", or &apos; and &quot;.
      • < and > always need to be escaped as &lt; and &gt; if you literally want them to render as ‘<‘ and ‘>’ in the text.
      • Ampersand (&) is a little more complicated.
        • Ampersand followed by whitespace renders as ampersand.
        • Ampersand followed by one or more characters that don’t form a valid HTML entity code render as Ampersand followed by those characters. So… &qqq; renders as &qqq;, but &lt;1 renders as <1.

    Example:

    <string name="nice_html">
    <![CDATA[
    <p>This is a html-formatted \"string\" with <b>bold</b> and <i>italic</i> text</p>
    <p>This is another paragraph from the same \'string\'.</p>
    <p>To be clear, 0 &lt; 1, & 10 &gt; 1<p>
    ]]>
    </string>
    

    Then, in your code:

    TextView foo = (TextView)findViewById(R.id.foo);
    foo.setText(Html.fromHtml(getString(R.string.nice_html), FROM_HTML_MODE_LEGACY));
    

    IMHO, this is several orders of magnitude nicer to work with 🙂


    August 2021 update: My original answer used Html.fromHtml(String), which was deprecated in API 24. The alternative fromHtml(String,int) form is suggested as its replacement.

    FROM_HTML_MODE_LEGACY is likely to work… but one of the other flags might be a better choice for what you want to do.

    On a final note, if you’d prefer to render Android Spanned text suitable for use in a TextView using Markdown syntax instead of HTML, there are now multiple thirdparty libraries to make it easy including https://noties.io/Markwon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this text inside a div with a fixed width: Some headline (2009-10-10
I have upgraded some VB6 code, which uses fixed length strings in custom types,
I have a directed cyclic graph. Some edges are FIXED and may not be
I have some classes layed out like this class A { public virtual void
I have some code like this in a winforms app I was writing to
I have several strings which look like the following: <some_text> TAG[<some_text>@11.22.33.44] <some_text> I want
I have string which i have to display in TextBlock, my TextBlock have some
I have an NSButton (Push Button) with some temporary title text built in Interface
I'm using a DevExpress ComboBoxEdit to select a string. Some of the strings are
I am trying to setup a few fixes in some code that have caught

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.