Public
Sep 03, 2015
Android pro tip: One-line custom fonts in XML with data binding!
I started playing with data binding last night for real, and I'm amazed by the power it gives you. Check out this one-liner to set a custom font on a TextView:
@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName){
textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
In XML:
<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Previously, this used to take endless lines of repetitive Java code (one for each TextView!). The missing custom typeface support in TextView has been a grievance held against the Android platform for years. There's even an inventive library to address it[2]. Data binding makes all of that obselete. If you can't tell, I'm in love!! It's terse, powerful, and one of the biggest leaps forward I've seen for Android productivity.
As usual, you need to put your font file in assets/fonts/, and make sure to include the data binding framework.
EDIT: I've published FontBinding, a full example of using this technique with a lazy-loading font cache, on Github!
https://github.com/lisawray/fontbinding
[1] https://developer.android.com/tools/data-binding/guide.html
[2] Calligraphy: https://github.com/chrisjenx/Calligraphy
I started playing with data binding last night for real, and I'm amazed by the power it gives you. Check out this one-liner to set a custom font on a TextView:
@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName){
textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
In XML:
<TextView
app:font="@{`Source-Sans-Pro-Regular.ttf`}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Previously, this used to take endless lines of repetitive Java code (one for each TextView!). The missing custom typeface support in TextView has been a grievance held against the Android platform for years. There's even an inventive library to address it[2]. Data binding makes all of that obselete. If you can't tell, I'm in love!! It's terse, powerful, and one of the biggest leaps forward I've seen for Android productivity.
As usual, you need to put your font file in assets/fonts/, and make sure to include the data binding framework.
EDIT: I've published FontBinding, a full example of using this technique with a lazy-loading font cache, on Github!
https://github.com/lisawray/fontbinding
[1] https://developer.android.com/tools/data-binding/guide.html
[2] Calligraphy: https://github.com/chrisjenx/Calligraphy

View 38 previous comments
+Mitch Ware sounds like a question for +Hugo Visser!19w
The latest version of android-apt should be OK, there have been fixes made that also affect databinding.19w
+Hugo Visser You sir are correct, thanks!
+Lisa Wray Now I'm having a separate problem: Custom fonts work within an Activity (specifically a custom TextView within the Toolbar), but I can't seem to get custom fonts working within a Fragment's RecyclerView list items. Is there any additional calls to DataBindingUtil that need to happen in the fragment view inflation? Or within the RecyclerView list adapter? 19w
Great lib. The only problem i see is the preview does not show the font.
(when we use font icon it is really a problem)7w
+Mitch Ware sorry for late reply! You probably figured this out by now, but you need to inflate the layout of each of the list items (in onCreateViewHolder) using data binding. +Antoine ARNOULT I don't think it's ever been possible to preview custom fonts, right? Hopefully the tooling will add support for previewing custom attributes in the future. 7w
In fact, I've just found this thread http://stackoverflow.com/a/35266586/1646479 and it works7w