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
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:
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 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
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);
Add custom font for complete android application
I figured it out by my self. This is the code I used. I create custom TextView
which has custom font as default font.
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
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.
Related Topics
How to Change the Led Intensity of an Android Device
How to Default to Numeric Keyboard on Edittext Without Forcing Numeric Input
Memory Analyzer Tool in Android
Broadcastreceiver for Location
Android Studio Build.Gradle Warning Message
Getting Nosuchfielderror Instance Org/Apache/Http/Message/Basicheadervalueparser
Getting Permission Denial Exception
Android Viewpager Implementation
Gradle Sync Failed Could Not Find Constraint-Layout:1.0.0-Alpha2
Android: Fragments Overlapping Issue
Downloading/Caching Google Maps for Offline Use
Android: Permission Denial: Starting Intent with Revoked Permission Android.Permission.Camera
How to Start an Activity from Within a Fragment
Get Battery Level Before Broadcast Receiver Responds for Intent.Action_Battery_Changed