0

I have a problem while testing fonts I supply in an asset/fonts folder. I simply placed some TTF font files in this folder. They were chosen from fonts in my font library without caring about their provenance. Some of them were my own fonts(GAMZ). The files are scanned by the class fontProvider which maintains brief records of the fonts in an ArrayList.

public class Font {
int style;
String file;

public Font(String file, int style) {
    this.style = style;
    this.file = file;
}

public String name() {
    int ixdot = file.indexOf('.');
    String g;
    if (ixdot < 0) g = file;
    else g = file.substring(0, ixdot);
    return g;
}
}

public class FontProvider {
private ArrayList<Font> fontlist;
private MainActivity mainactivity;

public FontProvider(MainActivity mainactivity) {
    this.mainactivity = mainactivity ;
    getFonts();
    sysoutfonts(); // not supplied but lists the fonts with System.out calls
}

public void getFonts() {
    String fontfiles[] = null;
    AssetManager assetManager = mainactivity.getAssets();
    Typeface tf = null;
    try {
        fontfiles = assetManager.list("fonts");
    } catch (IOException e) {}

    for (String file : fontfiles) {
        tf = Typeface.createFromAsset(assetManager, "fonts/" + file);
        if (tf != null) {
            Font f = new Font(file, tf.getStyle());
            fontlist.add(f);
        }
    }
    fontlist.trimToSize();
}
}

There are other methods in fontProvider but they are not relevant and not shown here. When run on a USB connected API 19 tablet all is OK and sysoutprint() reports, on my adblog:

Chalk (Normal), GAMZ One (Bold), GAMZ Teacher's Pet Printed Cards (Bold), GAMZ Teacher's Pet Rounded (Bold), GAMZ Two (Bold), Kids (Normal), Southern Normal(Normal)

However, when I try it on my API 23 tablet, it crashes. Always being neurotic about any routine that involves external resources, I applied a try/catch to the Typeface.createFromAsset(...) call. ie:

try {
     tf = Typeface.createFromAsset(assetManager, "fonts/" + file);
} catch (Exception e) {
  System.out.println("Exception e: " + e.getMessage());
  }

The API 23 tablet no longer crashes bout sysoutprint() reports:

GAMZ One (Bold), GAMZ Teacher's Pet Printed Cards (Bold), GAMZ Teacher's Pet Rounded (Bold), GAMZ Two (Bold), Kids (Normal)

Chalk and Southern are missing! Instead the e.getMessage() reports, for these TTF files:

Exception e: Font asset not found fonts/Southern Normal.ttf

So: No error in API 19 but these fonts won't load into a Typeface in API23!

Has there been some subtle change to the API implementation, since Level 19, of the Typeface.createFromAsset() method(), even a bug?

PS Typeface.createFromFile() behaves in the same way.

PPS. I'm sure I can find a workaround for this by choosing different, but similar, fonts for my final app, even if that involves designing the fonts myself.

Kyll
  • 7,036
  • 7
  • 41
  • 64
JosieH
  • 85
  • 10
  • please see this http://stackoverflow.com/questions/13539688/how-to-use-roboto-font-in-android-project – Farmer Oct 20 '16 at 09:32
  • Thank you @Shailesh Limbadiya, I don't see how the link you supplied immediately helps me. I Android 19, Typefaces can be created from Chalk and Southern TTFs but NOT in Android 23. Typefaces can be created from all the other TTFs I supplied. Once the good typefaces are listed in my Fonts class, I can use them exactly how, and when, I want. It is not their usage that is the problem, but why do some fonts not work in Android 23 when they are perfectly OK in Android 19? Meanwhile, I have replaced them with Staccato 555 and Souvenir which both work in Android 23 and 19. :) – JosieH Oct 20 '16 at 21:53
  • NP @JosieH when you read the documentation then you know that what changes come after api level >=23, for example we declare the permission in menifest.xml file then also we need to get run time permission in api level is >=23. So Please read the documetation – Farmer Oct 21 '16 at 05:25
  • Got it. Thank you :) Shailesh Limbadiya – JosieH Oct 21 '16 at 21:57
  • @Shailesh Limbadiya I am admonished for not reading properly... Forgive an old woman. – JosieH Oct 21 '16 at 22:04

0 Answers0