Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Roboto and Roboto Bold guaranteed to be available on 4.0+?

In our application, we're using Roboto and Roboto Bold. However, in some versions of Android (seems to be 4.0 to 4.1) we have issues with text rendering when using the imported version of Roboto (i.e. using Typeface.createFromAsset()) that do not appear when simply using the built-in version of Roboto (i.e. Typeface.DEFAULT).

I know that Roboto and Roboto Bold were introduced in Android 4.0, but I can't seem to find anything that guarantees that these fonts are available regardless of manufacturer modification (e.g. Touchwiz, Sense). If they are guaranteed to exist, we can just use a version check to only use the custom import for devices lower than Android 4.0.

like image 808
Kevin Coppock Avatar asked Oct 05 '22 21:10

Kevin Coppock


1 Answers

EDIT: With some experimentation, particularly with the Galaxy S3 that allows a user to change their font, this is what I've discovered:

  • Using Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL) will return that CUSTOM typeface, rather than the system default sans-serif font (i.e. Roboto)
  • Instead, use Typeface.create("sans-serif", Typeface.NORMAL) (or BOLD) and it will return Roboto regardless of the user's font customization. From the list below, you can actually use "helvetica", "tahoma", "verdana", or "arial" above instead of "sans-serif" with the same result.

I found a document called system_fonts.xml that seems to confirm that Roboto will be used for any reference to Typeface.SANS_SERIF in the SDK directory under:

platforms > android-14 > data > fonts

<!--
    System Fonts

    This file lists the font families that will be used by default for all supported glyphs.
    Each entry consists of a family, various names that are supported by that family, and
    up to four font files. The font files are listed in the order of the styles which they
    support: regular, bold, italic and bold-italic. If less than four styles are listed, then
    the styles with no associated font file will be supported by the other font files listed.

    The first family is also the default font, which handles font request that have not specified
    specific font names.

    Any glyph that is not handled by the system fonts will cause a search of the fallback fonts.
    The default fallback fonts are specified in the file /system/etc/fallback_fonts.xml, and there
    is an optional file which may be supplied by vendors to specify other fallback fonts to use
    in /vendor/etc/fallback_fonts.xml.
-->
<familyset>

    <family>
        <nameset>
            <name>sans-serif</name>
            <name>arial</name>
            <name>helvetica</name>
            <name>tahoma</name>
            <name>verdana</name>
        </nameset>
        <fileset>
            <file>Roboto-Regular.ttf</file>
            <file>Roboto-Bold.ttf</file>
            <file>Roboto-Italic.ttf</file>
            <file>Roboto-BoldItalic.ttf</file>
        </fileset>
    </family>

Since the vendor fonts must be placed in fallback_fonts.xml and the system fonts will always be prioritized, and the first family listed is Roboto under the aliases of sans-serif, aria, helvetica, tahoma, or verdana, unless I find out otherwise I think it's safe to assume that Roboto will be the font returned for a call to Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL).

I'm still going to leave this open for now, hoping for a definitive answer, as I'm unsure whether an OEM is allowed to modify system_fonts.xml. If they are, then this isn't really helpful at all.

like image 80
Kevin Coppock Avatar answered Oct 10 '22 01:10

Kevin Coppock