Use external fonts in android

AndroidFontsAndroid Widget

Android Problem Overview


I want to use external fonts in my app. I have tried adding new fonts using AssetManager but it did not work. Below is my code:

Typeface face;

face = Typeface.createFromAsset(getAssets(), "font.otf");

textview.setTypeface(face);

but its not showing the text...

Please help me with this.

Android Solutions


Solution 1 - Android

AFAIK, Android does not support OpenType. Use a TrueType font instead.


UPDATE: Apparently OpenType is now supported, at least somewhat. It was not supported originally, so you will want to test your font thoroughly on whatever versions of Android your app will support.

Solution 2 - Android

In order to access our font easily, we need to bundle it with our application in a way that our code can subsequently load it. To do this, we create a Fonts folder in our assets direct

This may be your .xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/DefaultFontText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:text="Here is some text." />
<TextView
    android:id="@+id/CustomFontText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:text="Here is some text.">
    </TextView>

Write following code in your .java class

Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv = (TextView) findViewById(R.id.CustomFontText);
    tv.setTypeface(tf);

Solution 3 - Android

Android does support OTF (I'm not sure from which SDK version but it definitely works with 1.6), I was using a typewriter OTF font for a while but the rendering is nowhere near as accurate as with the TTF version I ended up using (via online font converter). The baseline was all over the place (some letters were a full 2 pixels higher than others), and on LDPI phones like the HTC Wildfire the problem is greatly magnified due to the larger pixels.

Solution 4 - Android

I was having the same problem. My font was not working in android either but I needed it to work. Using a font editor, I copied the characters from my font into the font that comes with the FontSampler example from Android-src-2_1. It worked perfectly.

While I will admit that my method was questionable from an intellectual property point of view, I didn't actually wind up using the original font, as all of the characters were replaced and all references to the old font where replaced as well. I had tried 'looking' at the way the two fonts were defined but making all the font variables match didn't work either. So in the ned, I used a skeleton of the original font as a template for the new font.

Solution 5 - Android

android supports both otf and ttf formats, i experienced both of them.

tv3 = (TextView)findViewById(R.id.tv1);
	Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/TRAJANPRO-BOLD.OTF");
	tv3.setTypeface(typeFace);

this is the step i used for both english and local languages

Solution 6 - Android

Use Fontinator it support booth OTF and TTF Fonts

It is an Android-Library make it easy, to use custom Fonts.

https://github.com/svendvd/Fontinator

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmuditView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidKirtikumar A.View Answer on Stackoverflow
Solution 3 - AndroidrichardleggettView Answer on Stackoverflow
Solution 4 - Androiddave evarttView Answer on Stackoverflow
Solution 5 - AndroiddeepanView Answer on Stackoverflow
Solution 6 - AndroidSven NählerView Answer on Stackoverflow