Use External Fonts in Android

How to use custom font in a project written in Android Studio

Update 2021:

Create a folder named font inside the res folder and copy your font

Sample Image

All font names must be only: lowercase a-z, 0-9, or underscore.

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/abc_font" />

For programmatic use:

textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))

For Android Studio 4.2+ there's even now a menu option:

Sample Image

How to add external fonts to android application

You need to create fonts folder under assets folder in your project and put your TTF into it. Then in your Activity onCreate()

TextView myTextView=(TextView)findViewById(R.id.textBox);
Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);

Please note that not all TTF will work. While I was experimenting, it worked just for a subset (on Windows the ones whose name is written in small caps).

How to import external font/ typeface in ANDROID STUDIO?

Go on your Project: app-->src-->main

Create a assets folder like this:

|assets

|-----------------fonts

|-------------------font.ttf

|java

|res

AndroidManifest.xml

and then use

     Typeface face=Typeface.createFromAsset(getAssets(),"fonts/digital.ttf");
txtV.setTypeface(face);

Android - Using Custom Font

On Mobiletuts+ there is very good tutorial on Text formatting for Android. Quick Tip: Customize Android Fonts

EDIT: Tested it myself now. Here is the solution. You can use a subfolder called fonts but it must go in the assets folder not the res folder. So

assets/fonts

Also make sure that the font ending I mean the ending of the font file itself is all lower case. In other words it should not be myFont.TTF but myfont.ttf this way must be in lower case

Use external fonts in 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.

How to add custom font in react native android

  1. Add your fonts file in
  • Project folder/android/app/src/main/assets/fonts/font_name.ttf

  1. Restart the package manager using react-native run-android
  2. Then you can use your font in your style e.g
  • fontFamily: 'font_name'

Cant use an external font

code seems perfect as per my reference, see link here

try to clean your project once and rebuild it.

How to set custom font for a whole application in Android?

Write a class

public class MyApp extends Application{
// Put the onCreate code as you obtained from the post link you reffered
}

now next thing is in AndroidManifest.xml for the application tag give name for your application class. In this case it is MyApp

<application
android:name=".MyApp"
...
>
...
</application>

So whenever the App is opened , onCreate method of MyApp class would be invoked , and the font would be set.

Update
Put font file under assets/fonts/your_font_file.ttf

Put this line under onCreate method of your application class(MyApp)

TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/your_font_file.ttf");

Source File for TypefaceUtil

public class TypefaceUtil {

/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
*
* @param context to work with assets
* @param defaultFontNameToOverride for example "monospace"
* @param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {

final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
newMap.put("serif", customFontTypeface);
try {
final Field staticField = Typeface.class
.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
try {
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
}

Now update your style.xml file

put the below line your style which is included for your activity in manifest file

  <item name="android:typeface">serif</item>

Hope this helps



Related Topics



Leave a reply



Submit