After walking you through seven blog posts on how to add custom fonts to your Android app, we'll show you a way which barely requires any work: using a library. There are quite a few custom font libraries out there, but we've limited this short introduction to our two favorites: Calligraphy & Fontain.
If you're interested in the implementation of your own custom fonts, check our previous blog posts:
Custom Fonts on Android Series Overview
Library #1: Calligraphy
A huge upside of Calligraphy is the fairly simple setup and that you can continue to use the regular TextView
class. Yes, you heard correctly: no custom view classes! We're assuming you still have the font .ttf
files in your /asset/
folder. If not, you better add them now.
Like every good library, Calligraphy comes as an easy to import gradle dependency. Add it to your app's build.gradle
:
dependencies {
compile 'uk.co.chrisjenx:calligraphy:2.1.0'
// your other dependencies
// ...
}
Next, if your Android project already extends Application
, add the Calligraphy initalizaion call to the onCreate()
method. If your app doesn't extend Application
yet, integrate the entire class. Make sure you update your AndroidManifest.xml
to the new application class though!
public class CustomFontApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// initalize Calligraphy
CalligraphyConfig.initDefault(
new CalligraphyConfig.Builder()
.setDefaultFontPath("SourceSansPro-Regular.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
}
And one last step before we can look at the usage: extend all of your activities to inject the context to Calligraphy:
public class CustomFontActivity extends AppCompatActivity {
// pass context to Calligraphy
@Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(context));
}
// other method calls of your activity
}
As already promised, you don't have to create a custom view to use your own fonts. You can use a regular TextView
:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visit futurestud.io"
fontPath="SourceSansPro-Regular.ttf"/>
With Calligraphy you set the font via the fontPath
. Please make sure that you understand that the fontPath
is not using any prefixes! Neither android:
, nor app:
.
If you're using Calligraphy, you can also set the custom font via the styles or even themes. If we've convinced you that this library is worth a look, and we think so, check out the extended guide on their Github.
Library #2: Fontain
The second library we wanted to introduce here is Fontain. Fontain offers a lot more than just the regular custom fonts. It lets you select weight, width, slope and more!
But, with more functionality comes a more complex setup and a longer introduction guide. This would require an entire mini series on its own and would have blown up this blog post too much. If you're interested in full flexibility with the styling of your custom fonts, give Fontain a look!
If you're not convinced of either one, check the font list on android-arsenal.com, which provides a ton of other custom font libraries.
Do you've a favorite custom font library? Let us know in the comments or on twitter @futurestud_io.