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 t
So if I want to create a custom view component that use a custom font how can I access the resource?
Your code would access it the same way that it does not. You will simply have to tell reusers of your custom view to include the font file in their assets.
Can't I redistribute my component with my favorite font !!!!
Sure you can. Put the font in a ZIP file along with the rest of your library project, along with instructions for where to place it in a project. Be sure to use a font that you have rights to redistribute this way, though.
Intellij Idea (and android studio as it's based on intellij) has a feature that let you include the asset files of the library module to application module, I don't know about other environment.
Go to project structure in file menu, then facets, choose application module, in compiler tab check "include assets from dependencies to the into APK" checkbox.
As intellij is far better than Eclipse, I think migrating is reasonable.
EDIT:
Asset and manifest merging are fully supported in android studio.
Here's my modified version/improvement on @Mr32Bit's answer. On the first call, I copy the font to the app private dir /custom_fonts. Future reads check if exists and if so reads existing copy (saves time on coping file each time).
note: This assumes only a single custom font.
/**
* Helper method to load (and cache font in app private folder) typeface from raw dir this is needed because /assets from library projects are not merged in Eclipse
* @param c
* @param resource ID of the font.ttf in /raw
* @return
*/
public static Typeface loadTypefaceFromRaw(Context c, int resource)
{
Typeface tf = null;
InputStream is = c.getResources().openRawResource(resource);
String path = c.getFilesDir() + "/custom_fonts";
File f = new File(path);
if (!f.exists())
{
if (!f.mkdirs())
return null;
}
String outPath = path + "/myfont.ttf";
File fontFile = new File(outPath);
if (fontFile.exists()) {
tf = Typeface.createFromFile(fontFile);
}else{
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);
}
catch (IOException e)
{
return null;
}
}
return tf;
}