Is it possible to set a custom font in an Android application?
I tried what is posted here, but I don't know where my extends Application
class is...
Any help?
EDIT:
I tried the following:
- Add an assets folder and insert the font inside as seen here:
Add a new class that extends from
Application
Call this new class from my
AndroidManifest.xml
.I went to my style and added it.
MyApp.java:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
FontsOverride.setDefaultFont(this, "DEFAULT", "raleway_regular.ttf");
// This FontsOverride comes from the example I posted above
}
}
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".MyApp"
android:theme="@style/AppTheme">
....
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:fontFamily">default</item>
</style>
But my font is still not changning... any idea?
Then the MyApp
class is called. But no effect on my fonts...
EDIT2: I realized that my buttons apply the custom font after I set a custom style for my buttons. Here is my custom button style:
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
And here is how it looks now:
So: my button is applying the style, but not the TextView
. Any idea on why my custom font is not being applied for all items in application?
res
then fromnew
option chooseAndroid Resource Directory
and selectfont
formResource type
and press OK, this will create font directory to placing your fonts in it..ttf
font files in there.Go to project style file which exists in following path
res/values/styles.xml
and add styles like that:Go to your code and write a class like this for changing whole app font:
Because fonts should separately set for each activities, I prefer write a class for it for more clean code.
Then call the class method before activity onCreate
super.onCreate(savedInstanceState)
:Remember that if you want changing font during runtime, write a chosen font in
SharedPreferences
or etc, then pass chosen font to all activitiessetFont
like above.I have tried font override too but at the end it's not a reliable solution, sadly overriding fonts take more than that. You can either wait for Android O with custom font to comes out or use a third party library.
The last solution that I came across was this library Caligraphy , which was easy to initiate and let you use as many font as you want. While checking its source code I understood why just overriding fonts won't work so even if you don't plan to use it I recommend reading through it once..
Good luck
EDIT
uk.co.chrisjenx:calligraphy
Lib is not more maintained for latest android version alternative is now https://github.com/InflationX/CalligraphyAdd your custom fonts to assets/
Usage
For default font
Define your default font using CalligraphyConfig, in your Application class in the #onCreate() method and pass it to the CalligraphyInterceptor that you add to your ViewPump builder.
Inject into Context: Wrap the Activity Context:
Custom Style
For Theme
Not more maintained by Developers Below Solution
There is a great library for custom fonts in android:Calligraphy
Here is a sample how to use it.
In Gradle you need to put this line into your app's build.gradle file:
And then make a class that extends Application and write this code:
You should have made on assets/ a "New Directory" "fonts" (see below), so in that code "your font path" should be "fonts/SourceSansPro-Regular.ttf". (It's just "fonts..." not "/fonts.." or "assets..")
And in the activity class put this method before onCreate:
And the last thing your manifest file should look like this:
And it will change the whole activity to your font! it's simple and clean!
On Assets you should right-click New Directory, call it "fonts". In the finder put the
.ttf
font files in there.Also dont forgot to add below two lines in attrs.xml,if you dont have attrs.xml file,create new file in values.xml
If you follow the first topic, this should work : Here
Yes with reflection. This works (based on this answer):
(Note: this is a workaround due to lack of support for custom fonts, so if you want to change this situation please do star to up-vote the android issue here). Note: Do not leave "me too" comments on that issue, everyone who has stared it gets an email when you do that. So just "star" it please.
You then need to overload the few default fonts, for example in an application class:
Or course if you are using the same font file, you can improve on this to load it just once.
However I tend to just override one, say "MONOSPACE", then set up a style to force that font typeface application wide:
API 21 Android 5.0
I've investigated the reports in the comments that it doesn't work and it appears to be incompatible with the theme android:Theme.Material.Light.
If that theme is not important to you, use an older theme, e.g.:
Create assets folder in main folder.add fonts folder in it. add your .ttf file in fonts folder.
Add following in your app Theme:
Create class as TypefaceUtil
Call it in application class or in the Launcher activity
A work around for kotlin will be this
Gist of the solution https://gist.github.com/Johnyoat/040ca5224071d01b3f3dfc6cd4d026f7
Step One
Put font file under assets/fonts/your_font_file.ttf and Create a kotlin class called TypeFaceUtil
Step Two Then in your on
onCreate
Step Three
add this to your style