TextView: How to italicise one word in a sentence

189 Views Asked by At

Hey guys =) android newbie here. I'm trying to write a sentence in TextView, and I only want one word in the sentence to be in italics. for e.g. android:text="my name isn't Amber?" ==> I want only the word "isn't" to be italicised. Any help would be appreciated. TIA!

2

There are 2 best solutions below

0
On

You can use the plain old html tags. This should do the trick:

String txt = "My name <i>isn't</i> Amber"; 
myTextView.setText(Html.fromHtml(txt,TO_HTML_PARAGRAPH_LINES_CONSECUTIVE));
0
On

One way to achieve that is using a Spannable, A StyleSpan with Typeface.ITALIC on isn't, in your case, and then setting the resulting CharSequence to the TextView. Another way is to use an Html string, enclosing isn't between <i></i>, and formatting it with Html.fromHtml