Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font in Android Library

Tags:

android

At the follow link

Android Dev Guide

is write:

Library projects cannot include raw assets The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.

So if I want to create a custom view component that use a custom font how can I access the resource? Can't I redistribute my component with my favorite font !!!!

Best regards

like image 626
Mr32Bit Avatar asked Sep 30 '11 12:09

Mr32Bit


People also ask

Which library supports font in Android?

Android O and AndroidX Library add support for Downloadable Fonts. Google Fonts is shipping a Font Provider in Google Play Services. This means Google Fonts are available to native apps on Android devices!

Where is the font folder in Android?

System fonts reside inside the /system/fonts directory. Before you begin tinkering with system files, you need to locate the . tff file for ROBOTO.

What is the default font family in Android?

"Roboto and Noto are the standard typefaces on Android and Chrome." From Wiki, "Roboto is a sans-serif typeface family developed by Google as the system font for its mobile operating system Android."


2 Answers

Here's a method for loading fonts from resources that actually works ;-) Credit to mr32bit for the first version.

private Typeface getFontFromRes(int resource)
{ 
    Typeface tf = null;
    InputStream is = null;
    try {
        is = getResources().openRawResource(resource);
    }
    catch(NotFoundException e) {
        Log.e(TAG, "Could not find font in resources!");
    }

    String outPath = getCacheDir() + "/tmp" + System.currentTimeMillis() ".raw";

    try
    {
        byte[] buffer = new byte[is.available()];
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath));

        int l = 0;
        while((l = is.read(buffer)) > 0)
            bos.write(buffer, 0, l);

        bos.close();

        tf = Typeface.createFromFile(outPath);

        // clean up
        new File(outPath).delete();
    }
    catch (IOException e)
    {
        Log.e(TAG, "Error reading in font!");
        return null;
    }

    Log.d(TAG, "Successfully loaded font.");

    return tf;      
}
like image 121
bk138 Avatar answered Sep 18 '22 22:09

bk138


Ok I have found a workaround for the problem. You need to copy the file to an external directory then load a typeface from file with Typeface.createFromFile and then delete the temporary file. I know is not a clean mode of work but is working grate.

1 - You need to put your font on "/res/raw/font.ttf"

2 - Inser in your code the following method

3 - put in your code Typeface mFont = FileStreamTypeface(R.raw.font);

4 - All is done

 Typeface FileStreamTypeface(int resource)
{
    Typeface tf = null;

    InputStream is = getResources().openRawResource(resource);
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/gmg_underground_tmp";
    File f = new File(path);
    if (!f.exists())
    {
        if (!f.mkdirs())
            return null;
    }

    String outPath = path + "/tmp.raw";

    try
    {
        byte[] buffer = new byte[is.available()];
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath));

        int l = 0;
        while((l = is.read(buffer)) > 0)
        {
            bos.write(buffer, 0, l);
        }
        bos.close();

        tf = Typeface.createFromFile(outPath);

        File f2 = new File(outPath);
        f2.delete();
    }
    catch (IOException e)
    {
        return null;
    }

    return tf;      
}

if someone have an alternative I'm pleased to read it. Do you have to remember that this workaround is only for Android Libraries

Best regards

like image 37
Mr32Bit Avatar answered Sep 22 '22 22:09

Mr32Bit