How to use custom font in android library project? - android

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

Related

how to use font family like professional in android studio

Is, any professional font family available on android. If it is not available, then how to use professional fonts in android. Thanks in advance
You can flow this step:
1.Go to the (project folder)
2.Then app>src>main
3.Create folder 'assets>fonts' into the main folder.
4.Put your 'abc.ttf' into the fonts folder.
AssetManager am = context.getApplicationContext().getAssets();
typeface = Typeface.createFromAsset(am,
String.format(Locale.US, "fonts/%s", "abc.ttf"));
setTypeface(typeface);
To add a font to your App, place the font file in asset folder and use the following code to set it to text view...
TextView textView = (TextView) findViewById(R.id.textView);
Typeface typeFace = Typeface.createFromAsset(getAssets(),
"fonts/digital.ttf");
textView.setTypeface(typeFace);
to get the fonts follow this link.
https://fonts.google.com
These are the google recommended fonts.

Integrate a specific font with Android apk

I have built an application which displays the news in Nepali (Devnagari) font.
Everything is fine but the problem is that the font is not displayed properly in some Android phones.
Here I have posted two images showing the variation of the same content in different phones. The first one is perfect but the second one is incorrectly displayed.
1st Image with correct fonts
2nd Image with incorrect fonts
The problem I realized is that the second phone does not contained the correct font installed in it.
How can we get rid of this problem?
I have got no proper idea regarding this, but a solution that came in my mind is that - What if we integrate the necessary font along with the apk file and the font too gets downloaded with the apk.
Please pass me suggestion for this and would be grateful to know how to implement any solution that exists.
If you are using the native Android Textview Component you could use the method:
public void setTypeface (Typeface tf)
And create the typeface this way :
static Typeface createFromAsset(AssetManager mgr, String path)
Create a new typeface from the specified font data.
Typeface.createFromAsset(getAssetManager(),"RELATIVE_PATH_TO_YOUR_TYPFACE_IN_THE_ASSET_FOLDER");
If you are using Android Studio the asset folder should be created under src/main/assets.
Make 1 folder in assets named "fonts", put your font files inside that folder. e.g. see following image
import android.graphics.Typeface;
Then declare Typeface object
Typeface allFontType, allFontTypeNormal, allFontTypeLight;
try
{
allFontType = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-Bold.ttf");
allFontTypeNormal = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-Medium.ttf");
allFontTypeLight = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Dosis-ExtraLight.ttf");
}
catch (Exception e1)
{
e1.printStackTrace();
allFontType = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
allFontTypeNormal = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
allFontTypeLight = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
}
To apply font in TextView use following code
TextView lblDaysName = (TextView)view.findViewById(R.id.lblDaysName);
lblDaysName.setTypeface(allFontTypeNormal);

How to access custom fonts from library project?

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");
}

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 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