Currently Android does not have a native View class that supports defining a custom Typeface in your layout’s XML. After inflating your layout you can obtain a reference to the TextView and call the setTypeface(android.graphics.Typeface)
method from your Java code, but this results in unnecessary duplication of logic and violates the
The solution is to write a custom TextView implementation with support for defining custom attributes.
The first thing you should do is create the file attrs.xml
in your project’s res/values
directory. Save the following XML to your new file:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TypefaceTextView"> <attr name="typeface" format="string" /> </declare-styleable> </resources>
This XML simply defines the custom attribute typeface
for the not yet existing class TypefaceTextView
. It also notes that the attribute value should be parsed as a string.
You can now create the new class TypefaceTextView.java
using the following source code:
You can see that the class simply looks for the custom typeface
attribute and attempts to load the named font from your application’s assets/fonts
directory at runtime. The class implements a static LruCache to avoid unnecessary hits to disk for fonts that were previously loaded. Note that if you’re targeting API levels prior to 12 the LruCache is only available using the Android support library.
Assuming you have the font file GothamBold-Family.otf
in your project’s assets/fonts
directory, then you can use the custom TypefaceTextView
class in your layouts like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:simple="http://schemas.android.com/apk/res/com.simple" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.simple.widget.TypefaceTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello, Gotham!" simple:typeface="GothamBold" /> </LinearLayout>
If you have another font you wish to use, simply update the value of the simple:typeface
attribute of your view. In this way you can use the same class to display a variety of custom typefaces in your Android application.
It’s not wroking for me (Android API 8). First when i type “xmlns:simple=”http://schemas.android.com/apk/res/com.simple” (I have named my package the same as you) I’m getting an error: “No resource identifier found for attribute “typeface” in package com.simple”. So I rename it to “xmlns:simple=”http://schemas.android.com/apk/res/com.simple.tvplus”
And now it’s okay but when i run it on my device i’m getting only errors:
12-17 14:02:54.349: E/AndroidRuntime(9681): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.artest.tvplus/com.artest.tvplus.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.artest.tvplus.TypefaceTextView
Can you help me ? Thanks in advance
Make sure you created your `attrs.xml` file as shown above. If you need additional help I suggest you try posting on stackoverflow.com.
Pingback: Building a Greek Lexicon and Grammar for Android » Ben Linskey
Thank you very much…
Nice explanation than other tutorials I read..