Is it possible to have multiple styles inside a TextView? Is it possible to have multiple styles inside a TextView? android android

Is it possible to have multiple styles inside a TextView?


In case, anyone is wondering how to do this, here's one way: (Thanks to Mark again!)

mBox = new TextView(context);mBox.setText(Html.fromHtml("<b>" + title + "</b>" +  "<br />" +             "<small>" + description + "</small>" + "<br />" +             "<small>" + DateAdded + "</small>"));

For an unofficial list of tags supported by this method, refer to this link or this question: Which HTML tags are supported by Android TextView?


Try Html.fromHtml(), and mark up your text with bold and italic HTML tags e.g:

Spanned text = Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff");textView.setText(text);


Slightly off-topic, but I found this too useful not to be mentioned here.

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))); 

I did not expect this to work. But it did.

Hope it's useful to some of you!