5
Textview label = (TextView) findViewById(R.id.item_title);
label.setText("Solve My Issue !");
Log.d("TAG","Font-Family : "+ String.valueOf(label.getTypeface()));

When i see the log it return Font-Family : android.graphics.Typeface@7f37f870

How to know the name of the font family ? Is it possible ?

Minion Dave
  • 143
  • 4
  • 11

1 Answers1

10

getTypeface() method returns the Typeface of label, while this Tyepface instance is a value of Map<String, Typeface> sSystemFontMap, which is a static field of Typeface. So now you get the value, and with reflection you can get the map sSystemFontMap, then want to find the key, which is exactly the name of font.

protected Map<String, Typeface> getSSystemFontMap() {
    Map<String, Typeface> sSystemFontMap = null;
    try {
        //Typeface typeface = Typeface.class.newInstance();
        Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);
        Field f = Typeface.class.getDeclaredField("sSystemFontMap");
        f.setAccessible(true);
        sSystemFontMap = (Map<String, Typeface>) f.get(typeface);
        for (Map.Entry<String, Typeface> entry : sSystemFontMap.entrySet()) {
            Log.d("FontMap", entry.getKey() + " ---> " + entry.getValue() + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sSystemFontMap;
}

private static List<String> getKeyWithValue(Map map, Typeface value) {
    Set set = map.entrySet();
    List<String> arr = new ArrayList<>();
    for (Object obj : set) {
        Map.Entry entry = (Map.Entry) obj;
        if (entry.getValue().equals(value)) {
            String str = (String) entry.getKey();
            arr.add(str);
        }
    }
    return arr;
}

I tested it, and the List arr contain below String

sans-serif
tahoma
arial
helvetica
verdana

That's not strange, because Android system use one same font as above five names. (Maybe some differences in various system version, for more information goto /system/etc/fonts.xml)

<family name="sans-serif">
    <font weight="100" style="normal">Roboto-Thin.ttf</font>
    <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
    <font weight="300" style="normal">Roboto-Light.ttf</font>
    <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
    <font weight="400" style="normal">Roboto-Regular.ttf</font>
    <font weight="400" style="italic">Roboto-Italic.ttf</font>
    <font weight="500" style="normal">Roboto-Medium.ttf</font>
    <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
    <font weight="900" style="normal">Roboto-Black.ttf</font>
    <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
    <font weight="700" style="normal">Roboto-Bold.ttf</font>
    <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
</family>
<!-- Note that aliases must come after the fonts they reference. -->
<alias name="sans-serif-thin" to="sans-serif" weight="100" />
<alias name="sans-serif-light" to="sans-serif" weight="300" />
<alias name="sans-serif-medium" to="sans-serif" weight="500" />
<alias name="sans-serif-black" to="sans-serif" weight="900" />
<alias name="arial" to="sans-serif" />
<alias name="helvetica" to="sans-serif" />
<alias name="tahoma" to="sans-serif" />
<alias name="verdana" to="sans-serif" />

from this you can see. sans-serif,tahoma,arial,helvetica,verdana are same thing. different names of font family sans-serif

liao
  • 156
  • 1
  • 5
  • it return me **sans-serif, tahoma, arial, helvetica, verdana** so what it means exactly ? – Minion Dave Jun 12 '16 at 08:12
  • In `/system/etc/fonts.xml`, Android system define these five names refer to 'Roboto' font . So sum up, **the font is `Roboto`** – liao Jun 12 '16 at 09:18
  • 1
    Is there any simpler way of doing this ? As I can see in the official documentation for Typeface, unfortunately, there is no property or method called Typeface.NAME or Typeface.getName() – Ashok Bijoy Debnath Sep 16 '19 at 10:37
  • Thanks! This works well, but there's some warning. Is there any way to fix these warning? 1. Cannot resolve field 'sSystemFontMap' at Typeface.class.getDeclaredField("sSystemFontMap"); 2. Unchecked cast: 'java.lang.Object' to 'java.util.Map' at (Map) f.get(typeface); – Fisher Jan 31 '21 at 15:18
  • It's utterly insane that this is the only way to do this simple task. – blimpse Feb 28 '23 at 16:48