Html tags in Text not working in TextView

Here I try to explain the details why Html tags in Text not working in TextView. At times you may need to display text with Bold and normal style in TextView like this,  …(You may skip to solution )

Html Tags in Text

Welcome, to your favorite music app store.   

So, you’ll define a string in string.xml file as follows

<string name="welcome_text"><b>Welcome,</b> to your favorite music app store.</string>

You’ll apply this wecome_text in a TextView in Java code as below.

TextView tvWelcomeUser=(TextView)findViewById(;

If you apply setText(int resID) as shown above you can see the text in TextView as expected “Welcome, to your favorite music app store.”

But if you like to display dynamically generated text after Welcome message, as shown below

Welcome, to your favorite music app store. Logged in as: <username>

here <username> is dynamically generated text. You can do this in two ways,

One way is concatenate dynamic text using “+” symol

tvWelcomeUser.setText(getString(R.string.welcome_text) + " Logged in as:" + username);

Another way is defining string in strings.xml file

//in string.xml file
<string name="welcome_text"><b>Welcome,</b> to your favorite music app store. Logged in as: %1$s.</string>

You’ll assign to TextView in Java code like this

//and in Java code

Here comes the issue, In both ways you’ll miss bold text for Welcome text.

even if you try this Html.fromSting(string) method it wont work.

//and in Java code

You may wondering about this issue, why its not working suddenly which worked before. The reason for this issue is you are converting to String which ignores html tags.

Html tags in Text not working in TextView Solution:

The only solution for this issue is using CData sections for the string in strings.xml file as shown below

//in string.xml file
<string name="welcome_text"><![CDATA[<b>Welcome,</b> to your favorite music app store. Logged in as:]]> %1$s.</string>

Now in Java code you can assign like this,

//and in Java code
String welcomStr=String.format(getString(R.string.welcome_text),username);

CData section in string text keeps the html tag data intact even after formatting text using String.format method. So, Html.fromHtml(str) works fine and you’ll see the bold text in Welcome message.

Welcome, to your favorite music app store. Logged in as: <username>

It displays bold text as expected. horray… 🙂

Hope it helps somebody… 😎

You may be also interested in


Apply ColorStateList for TextView to show when click on it

Using ColorStateList for Textiew we can display colours when click on it.

When we need to display a text as a link, and clicked on the text starts another activity or do some action. It looks good if we display text color states, so User can know text is clicked. Here is how we can do it…

Create file in res/drawable/text_selector_red_black.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
    <item android:state_focused="true" android:state_pressed="false" android:color="@color/black" />
    <item android:state_focused="true" android:state_pressed="true" android:color="@color/black" />
    <item android:state_focused="false" android:state_pressed="true" android:color="@color/black" />
    <item android:color="@color/red" />

and in color.xml file add these colors


Then use apply textColor property for TextView as “@drawable/text_selector_red_black” in design window. Or you can apply in coding as below


Hope this helps. 🙂

HTML Tags Supported By TextView, Android

Following are the Supported HTML Tags by TextView

  • <a href=”…”>
  • <b>
  • <big>
  • <blockquote>
  • <br>
  • <cite>
  • <dfn>
  • <div align=”…”>
  • <em>
  • <font size=”…” color=”…” face=”…”>
  • <h1>
  • <h2>
  • <h3>
  • <h4>
  • <h5>
  • <h6>
  • <i>
  • <img src=”…”>
  • <p>
  • <small>
  • <strike>
  • <strong>
  • <sub>
  • <sup>
  • <tt>
  • <u>


ref link.