How to access custom fonts from library project? - android

I had created an android library project.I Used custom fonts from my assets folder in my xml. But I can't access fonts from asset folder of the library project after integrated it with another project.I can access it from asset folder of new project.When i used with library project, it force closed and shows the error "native typeface cannot be made". How can I access asset folder fonts from library project ?? Is it possible ?? Thanks in advance.

Your description is not clear, you should provide your code.
To access fonts from assets, you need to put the method inside the onCreate() method, then you can create your own typeface with the font in assets/fonts folder
protected void onCreate(Bundle savedInstanceState) {
Typeface myFont = Typeface.createFromAsset(getAssets(),
"fonts/font.ttf");
}

Related

How to use custom font in android library project?

I have created a TextView library project where I have used a custom font. I have to create this library because this TextView will use in other three project as well. I found that assets/fonts folder is not supported by android library project. If I use this way it gives me error message
native typeface cannot be made at android.graphics.Typeface
Is there any workaround here to solve this issue. Other way, I have to use duplicate view in three different app. And also the sample code I am using to get font from library assets folder.
public static Typeface getFont(Context c, String fontName) {
synchronized (fonts) {
if (!fonts.containsKey(fontName)) {
Typeface t = Typeface.createFromAsset(
c.getAssets(), "fonts" + File.separator + fontName);
fonts.put(fontName, t);
}
return fonts.get(fontName);
}
}
infect you can do lot of things .one of easiest solution is following
you can write a function that copy paste font in some sd card folder and for sure it will be static . now you can create type from that path
createFromFile(String path)
http://developer.android.com/reference/android/graphics/Typeface.html
To use different Fonts in my project I have use font Library. Just add this library in gradle.
dependencies {
compile 'com.vstechlab.easyfonts:easyfonts:1.0.0'
}
Use of this library is very easy. I have use reference https://android-arsenal.com/details/1/2044
create fonts directory in your assets folder and set your font with ttf format in the font directory. then call font by this code:
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/font.ttf");
textViewName.setTypeface(tf);
your font must be here :
assets>fonts>font.ttf

Assets folder in Android Studio?

Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts. I'm using Android Studio and it doesn't seem like I have a assets folder. So I created one. But my app crashes when I place the assets folder in src/main. Im using this code to load my fonts.
Typeface fontRegular = Typeface.createFromAsset(getAssets(), "fonts/DroidSans.ttf");
Typeface fontBold = Typeface.createFromAsset(getAssets(), "fonts/DroidSans-Bold.ttf");
myDeviceModelTxt.setTypeface(fontRegular);
What am I doing wrong?
I am not sure if there has been any bug fixes since this was asked, but I am using the current structure for a project in Android Studio 0.5.2:
root-module
|--.idea
|--app
|----build
|----src
|------main
|--------assets
|----------SomeFont.ttc
|----------AnotherFont.otf
|--------java
|----------source code here
|--------res
|------AndroidManifest.xml
|----build.gradle
And then obtain it by calling
Typeface.createFromAsset(mContext.getResources().getAssets(), "SomeFont.ttc");
Word of caution though, there is a bug (https://code.google.com/p/android/issues/detail?id=9904) that prevents typefaces from being garbage collected properly. Use a singleton!
Create Assets folder Right click on app->>new->>Folder->>AssetsFolder like below image
Put your font inside this folder by just copy and paste. and use below code for example..
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "YourFontName.ttf");
setTypeface(tf);
Simply follow this path:
File > New > folder > assets Folder
Here App must be selected before creating folder.
For more information see this answer
The assets folder should be placed under the root of the project.
See here for more examples.

Android Studio - Set custom fonts with asset manager in fragment

I'm rewriting a project of mine to incorporate fragments for use on tablets. I also decided to start using Android Studio with the conversion. Pretty straight forward but I've run into an issue on the pretty simple task of setting a custom font.
I started with these:
Custom fonts in android and
List of files in assets folder and its subfolders and this Set custom font for Android fragments
In Eclipse my class extended Activity and I did the following in the OnCreate with no problems.(Having a directory and file "assets/FUT_R.ttf")
TextView tv=(TextView)findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getAssets(), "FUT_R.ttf");
tv.setTypeface(font);
now trying to convert this Activity to an ActionBarActivity (fragment with V7 support library) and I've changed the above code to this. (where view is the inflated layout with one Framelayout for phones)
TextView tv=(TextView) view.findViewById(R.id.someTextView);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "FUT_R.ttf");
tv.setTypeface(font);
and it crashes at run time with the lovely java.lang.RuntimeException: native typeface cannot be madeeven though i created and 'assets' directory on my new project with FUT_R.ttf in its root.
To confirm the asset manager I tried this code:
String[] f = null;
try {
f = getActivity().getAssets().list("");
}
catch (IOException e){
e.printStackTrace();
}
for(String f1:f){
Log.i("names",f1);
}
And got the following output:
07-26 07:40:40.134 2114-2114/com.myapp I/names: images
07-26 07:40:40.134 2114-2114/com.myapp I/names: sounds
07-26 07:40:40.134 2114-2114/com.myapp I/names: webkit
I'm confused because no where in my 'assets' directory or project do I have these files and/or directories. Obviously the error is the system can't find my font file. WHAT AM I DOING WRONG? Any direction would be greatly appreciated, I've wasted far too much time on this stupid problem.
Thanks!
I finally stumbled upon this: Load a simple text file in Android Studio
which points out the assets folder in Android Studio has apparently been changed. Instead of its normal location in the root app folder (same level as libs, src, ect) it needs to be created under yourapp/src/main/assets. Hopefully this helps someone else. I wasted a lot of time on it!

Android Application to Install a font

I wanna create an application in android to install a Hindi Font. Can some one guide me how i can go about it ? is it possible ?
Add the font file you want to use in the assets folder of your Android application. Then you can load the font and use it like this:
AssetManager assetManager = getContext().getAssets();
Typeface tf = Typeface.createFromAsset(assetManager,"GILB.TTF");
TextView logo = (TextView)findViewById(R.id.logo);
logo.setTypeface(tf);
In this case, I've used the true type font Gill Sans Bold.
You cannot install a font on a device.
You can package a font as an asset with your application to be used by your application.

Android develop LCD font

How to add additional font in android environment? I need to make a font like LCD with pale background numbers like this:
http://www.androidfreeware.net/img2/gps_speedometer_android_1.png
We at bangalore android developers group had done this for one of our project AutoMeter You can have a look at the code there in detail.
The code would look something like this
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),"DS-DIGIB.TTF");
waitingTimeView = (TextView) findViewById(R.id.WaitingTime);
waitingTimeView.setTypeface(myTypeface);
DS-DIGIB.TTF is the font put in the assets folder
public void setFontTextView(Context c, TextView name) {
Typeface font = Typeface.createFromAsset(c.getAssets(),"fonts/Arial.ttf");
name.setTypeface(font);
}
Download Arial font in ttf file formate and put in asset folder. Thats enough.
You want to use the android.graphics.Typeface class, which can create new Typefaces from assets or files.

Categories

Resources