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.
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 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
android:name=".MyApp"
...
>
...
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 newMap = new HashMap();
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
- serif
Hope this helps
How to apply custome fonts in android studio once for whole Project
THis is the answer which was posted by me on this Link. How to use custom font in Android Studio
Here we have a better way to apply fonts on EditTexts and TextViews on android at once and apply it in whole project.
First of All you need to make fonts folder. Here are Steps.
1: Go to the (project folder) Then app>src>main
2: Create folders named 'assets/fonts' into the main folder.
3: Put your fonts into the fonts folder. Here I Have 'MavenPro-Regular.ttf'
Here are the Steps for applying custom fonts on EditText and using this approach you can apply fonts on every input.
1 : Create a Class MyEditText (your preferred name ...)
2 : which extends EditText
3 : Apply your font
Here is code Example;
public class MyEditText extends EditText {
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyEditText(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf");
setTypeface(tf);
}
}
}
And in Here is the code how to use it.
MyEditText editText = (MyEditText) findViewById(R.id.editText);
editText.setText("Hello");
Or in Your xml File
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#fff"
android:textSize="16dp"
android:id="@+id/editText"
/>
How to set custom font in .xml file instead of .java file?
For your reference,
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();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/yourfont.ttf");
setTypeface(tf ,1);
}
}
In XML,
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Your text"
/>
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);
How to use desired font in Image Asset in Android Studio?
According to the documentation
You can use the fonts that are installed on your computer.
So you should first install the font, then you should be able to see it in the selector.
Install a font on Mac
Install a font on Windows10
Edit: found a duplicate of your question
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
Related Topics
Where Is Debug.Keystore in Android Studio
Integrate Mupdf Reader in an App
How to Have Onscrolllistener for a Scrollview
Listen to Volume Buttons in Background Service
Android: Broadcast Receiver for Screen on and Screen Off
How to Pass Arraylist<Customeobject> from One Activity to Another
Perform a Task on Uninstall in Android
How to Sort Firebase Records by Two Fields (Android)
How to Animate Recyclerview Items When They Appear
Error Type 3 Error: Activity Class {} Does Not Exist
Text Size and Different Android Screen Sizes
Android: How to Validate Edittext Input
How to Prevent Screen Capture in Android
Adding Custom Radio Buttons in Android
Custom Drawable For Progressbar/Progressdialog
Error:(1, 0) Plugin With Id 'Com.Android.Application' Not Found
How to Use Support Fileprovider For Sharing Content to Other Apps
What's the Enhancement of Appcompatactivity Over Actionbaractivity