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?
You can do it with the android SDK now, as the Android Developer official youtube channel published in set/2017 here.
Requires API Level 26 (Android 8 Oreo) and above.
You just need to put your font file in the
res/font
folder and reference them in yourTextView
in case you need a specific one with theandroid:fontFamily
attribute.If you want a base font on your whole app, simply put
in your
styles.xml
You can download a custom font from Android Studio and on the go if you want as well. All of this you can find with details on the video above.
Seems like you are using
android:fontFamily
instead ofandroid:typeface
in yourstyles.xml
.Try replacing
with
All I did was:
1: Added "new resource directory" to the RES folder, Selected RESOURCE TYPE as "font" from the drop-down given, named the new directory "font" and saved.
2: Added my "custom_font.ttf" to the FONT folder just created.
3: Added my custom font in the application base theme in STYLES.XML
DONE.
Write a class
now next thing is in AndroidManifest.xml for the application tag give name for your application class. In this case it is 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)
Source File for TypefaceUtil
Now update your style.xml file
put the below line your style which is included for your activity in manifest file
Hope this helps
Android provides simpler and best solutions which is supported over API level 14 by Support Library 26+. Fonts in XML It also supports font family option. Required import :
implementation "com.android.support:support-compat:<26+ version>"
Follow these simple steps and font family will be applied in your app without any hurdle:
Or use below to set typeface programmatically
First, make a new class that overrides whatever View you want to customize. (e.g. want a Button with a custom typeface? Extend
Button
). For example:Now, if you don't have one, add an XML document under
res/values/attrs.xml
, and add:Okay, so with that out of the way, let's get back to the
parseAttributes()
method from earlier:Now you're all set. You can add more attributes for about anything (you could add another one for typefaceStyle -- bold, italic, etc.) but now let's see how to use it:
The
xmlns:custom
line can really be anything, but the convention is what's shown above. What matters is that it is unique, and that's why the package name is used. Now you just use thecustom:
prefix for your attributes, and theandroid:
prefix for android attributes.One last thing: if you want to use this in a style (
res/values/styles.xml
), you should not add thexmlns:custom
line. Just reference the name of the attribute with no prefix: