Skip to content

TypefaceCompat

TR4Android edited this page Aug 4, 2016 · 3 revisions

This wiki highlights the setup of the TypefaceCompat utility provided with this library. Here's a quick overview of all the features available:

  • Easily setup and use the text styles recommended in the Typography section of the Material Design guidelines on all devices back to API level 7
  • Respect custom system typefaces while still maintaining the newest default typeface (Roboto) on all devices back to API level 14

1. Install the TypefaceCompatFactory

To properly apply the required typefaces the library needs to catch all (sub)classes of TextView during inflation. This is done using a custom LayoutInflatorFactory that takes care of reading the fontFamily attribute and loading the associated typeface. This needs to be installed your onCreate() method before the call to super.onCreate() (if you have a BaseActivity this might be a good place to put it):

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TypefaceCompatFactory.installViewFactory(this);
        super.onCreate(savedInstanceState);
        ...
    }
}

The above installs the TypefaceCompatFactory with the default options. There's also an extended constructor that allows you to customize some of the aspects of the TypefaceCompatFactory:

TypefaceCompatFactory.installViewFactory(Context context, boolean typefaceDetectionEnabled);

Here's a list of all options available along with a short explanation:

  • boolean typefaceDetectionEnabled: True if the factory should automatically detect the used system typeface and adjust its behavior properly. This makes sure that the newer Roboto typefaces are only used if no custom typefaces are applied by the system. Note: this only works starting with API level 14 and comes with a small performance penalty.

2. Style your text

Now you can use any of the TextAppearance.AppCompat.xxx styles provided by the AppCompat v7 Support Library and TypefaceCompat will take care of the rest. For example, to use the Medium 20sp Title style you'd declare the following TextView in your layout file:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

You can also use your own styles by using the attributes of the TextView and TypefaceCompat will detect those. So, for example, the following TextView would also have a Medium 20sp style:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-medium"
    android:textSize="20sp" />

However, due to undesired side effects, the line spacing defined in the Typography section is not automatically applied to the TextView and has to be manually set where it is required. The dimensions are given by the line_spacing_extra_xxx_material values that come with the library. For example, to have the proper Body 1 style with line spacing, you'd need to declare the following TextView:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lineSpacingExtra="@dimen/line_spacing_extra_body_1_material"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

3. Enjoy

Congrats, you're all set! Providing the correct text styles has never been easier. If you have any issues, bugs or feature requests please open a new issue.

Clone this wiki locally