11

I'm making an app where sometimes I want to change the font of a textview to italics, depending on the data to show.

On my Galaxy Nexus, it's simply a case of

textView.setTypeface(font, iWantItalics ? Typeface.ITALIC : Typeface.NORMAL);

and it works beautifully.

The problem is that I've got a new Galaxy Note II to test and... nope, no italics.

Reading Samsung devices supporting setTypeface(Typeface.Italic)? I get the impression that it's a bug on the Note's Android build, so the Roboto font simply has no italics. I've tried every advice on that thread and others similar (Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.create(null, Typeface.ITALIC), etc.) with no luck.

My problem is that the workaround the guy from that thread used was copying the Roboto TTF in the assets directory and creating the font from there but what about people with another default font in their phones? I don't want to force Roboto on them or, even worse, to have that other font when the typeface is normal and Roboto italics otherwise.

Has anybody an idea for me? Thanks.

Community
  • 1
  • 1
Serandel
  • 417
  • 5
  • 20
  • I created the question that you referenced. For what its worth I never found any other way to deal with the devices that didn't work correctly. Sorry for the bad news. I do hope someone comes forward with a better solution though =) – FoamyGuy Nov 28 '12 at 01:29
  • It's incredible to have such an stupid bug in such a high-end and brand new device, isn't it? – Serandel Nov 28 '12 at 13:51
  • I've got the attention of a developer in the Samsung forums, so there is hope. :) http://developer.samsung.com/forum/thread/typefaceitalic-not-working-on-galaxy-note-2/77/209984 – Serandel Nov 30 '12 at 14:05
  • 1
    On my Samsung Galaxy Tab 2, it doesn't work either. – thomaus Feb 04 '13 at 14:34
  • @thomaus See my answer, maybe that helps you too! – David Wasser Mar 12 '13 at 12:24

2 Answers2

14

As a workaround for this bug, you can set the text italic this way (which seems to work on the broken Samsung devices):

textView.setText(setTextStyleItalic(textView.getText());

and you need this method:

public static CharSequence setTextStyleItalic(CharSequence text) {
    final StyleSpan style = new StyleSpan(Typeface.ITALIC);
    final SpannableString str = new SpannableString(text);
    str.setSpan(style, 0, text.length(), 0);
    return str;
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
4

Another easy solution is to do it like this:

myTextView.setText(Html.fromHtml("<i>" + myString + "</i>"));
Ivo
  • 18,659
  • 2
  • 23
  • 35