Friday , March 22 2019
Home / Android / Custom Font in android

Custom Font in android


In this post, we’ll look at a generalized approach, which is more complex, but also more suitable for a repeated use of custom fonts.

Extending TextView

We’ll create a new Java class, which extends TextView. This allows us to use that class in all XML views. It inherits all functionality and properties of a regular TextView but adds our custom font.

The first three methods are just constructors, which we override to call a single method applyCustomFont(). That method is the important piece of the puzzle. Lastly, we’ve to call setTypeface() with the font and we’re almost done. In case you’re wondering, we can call the setTypeface() directly (and not on a TextView object), since we’re extending the TextView class.

Using the Class

You might wonder, if so much preparation is worth the effort. In this section you’ll see that it is indeed. Because all you’ve left to do is use the class in an XML view and it automatically has your custom font. There is no Java code necessary.

As you can see, you can continue to use all niceties (e.g. textSize, textColor) of TextView. Now, just replace all <TextView/> elements with the class we just created, for example <com.w2class.uicomponent.CustomTextVIew/> and you applied your custom font everywhere!

About admin

Check Also

Android Activity Launch Modes

Launch mode is an instruction for Android OS which specifies how the activity should be ...

Leave a Reply