0

i was wondering if someone knows or has a way to dynamically load all fonts .ttf and .otf from assets folder and classify it by family, i have a static way (which is not completely done), i have a method that returns a List with all the font families, hardcoded, so i have this other method to return a typeface given the font family (string) given and it's style:

public Typeface getTypeface(Context context, String name, int textStyle)
{
    String fontFileName = "";
    switch (name)
    {
        case "ComicNeue-Angular":
        {
            switch (textStyle)
            {
                case Typeface.NORMAL: {} break;
                case Typeface.BOLD: {} break;
                case Typeface.ITALIC: {} break;
                case Typeface.BOLD_ITALIC: {} break;
            }
        } break;
        case "ComicNeue":
        {
            switch (textStyle)
            {
                case Typeface.NORMAL: {} break;
                case Typeface.BOLD: {} break;
                case Typeface.ITALIC: {} break;
                case Typeface.BOLD_ITALIC: {} break;
            }
        } break;
        case "Impact":
        {
            switch (textStyle)
            {
                case Typeface.NORMAL: {} break;
                case Typeface.BOLD: {} break;
                case Typeface.ITALIC: {} break;
                case Typeface.BOLD_ITALIC: {} break;
            }
        } break;
        case "LiberationSans":
        {
            switch (textStyle)
            {
                case Typeface.NORMAL: {} break;
                case Typeface.BOLD: {} break;
                case Typeface.ITALIC: {} break;
                case Typeface.BOLD_ITALIC: {} break;
            }
        } break;
        case "Roboto":
        {
            switch (textStyle)
            {
                case Typeface.NORMAL: {} break;
                case Typeface.BOLD: {} break;
                case Typeface.ITALIC: {} break;
                case Typeface.BOLD_ITALIC: {} break;
            }
        } break;
    }
    return Typeface.createFromAsset(context.getAssets(), fontFileName);
}

i want to add all fonts and classify it by family, so i can load a spinner with all font families. so i don't have to add lines to this code if i add another font family to the folder. (I have to add like 7 more font families to the project)

any ideas?

thanks in advance!

Daniel Andujar
  • 1,184
  • 1
  • 11
  • 22
  • recommendation: don't try to break the Android development model and just bundle your fonts. If you want custom anything in your app, the Android way is to bundle that in yourself. You could, if this was a webview based app, use webfonts, but that would be a worse experience than instantly loading the bundled asset. – Mike 'Pomax' Kamermans Apr 04 '16 at 16:29
  • how do i bundle the fonts? i am actually looking for that and read it is not possible to bundle .ttf files – Daniel Andujar Apr 05 '16 at 01:20
  • I assume you found http://stackoverflow.com/questions/5634245/how-to-add-external-fonts-to-android-application already? – Mike 'Pomax' Kamermans Apr 05 '16 at 06:24
  • Yes, i am able to use an external font, i just want to classify different TTF or OTF by family, so when i set the typeface to Typeface.BOLD it selects the correct font file. – Daniel Andujar Apr 05 '16 at 14:06
  • then I have bad news for you: "bold" doesn't exist in the opentype spec, there's only "weight", and there is no official mapping between numerical weights and what they mean in human words. A font with weight 200 could be bold, or weight 900 could be bold - while CSS has rules that says 400 is normal, etc., Opentype does not have any such rules. – Mike 'Pomax' Kamermans Apr 05 '16 at 16:44
  • Any help? Someone? – Daniel Andujar Apr 07 '16 at 13:15

0 Answers0