15

I'm creating a custom View class that displays text using a StaticLayout with a custom typeface loaded from a .ttf asset file. The basic look of the typeface is a serif font and the TrueType properties in the font file indicate that it's serif. Nevertheless, for characters not in the font, Android falls back to a sans-serif typeface (it looks like Roboto or Noto, depending on the Android version).

Is there a way to control this so that the Android layout engine falls back to a serif typeface? (Like maybe some magic string to pass to Paint#setFontFeatureSettings(), or perhaps a way to specify a stack of typefaces for use in a Paint?)

Any solution would have to work when the app is distributed, so playing games with Android's system configuration files is not an option. Also, I'm aware of this question (that is still unanswered after four years), but my problem is a bit different, although the solution to both issues might very well be the same.

Community
  • 1
  • 1
Ted Hopp
  • 222,293
  • 47
  • 371
  • 489

1 Answers1

0

As of Lollipop, there's a hidden class called FontFamily and a hidden method called Typeface.createFromFamilies. If you're very careful, you can probably use this stuff via reflection or other trickery, but be prepared to fallback to the public API.

j__m
  • 8,784
  • 1
  • 26
  • 49
  • Unfortunately, as of Android Pie, the `FontFamily` class appears to be in the dark gray list, so isn't accessible even through reflection. (See https://developer.android.com/about/versions/pie/restrictions-non-sdk-interfaces). – Ted Hopp Aug 17 '18 at 20:21
  • Yes, time has proven the wisdom of my advice of being able to fall back on the public API lol. It's unfortunate that so much of the Android framework is hidden, often without any good reason. For example, there's a public method that creates a `ParcelFileDescriptor` that duplicates a `FileDescriptor`, but no public method that adopts one. That's functionality that has existed in `ParcelFileDescriptor` from the very beginning, but remains hidden to this day. Android is full of this nonsense. – j__m Aug 19 '18 at 04:42