Custom Fonts in Android - w2class
Peeyush Singhal
Senior Technical Lead at Mphasis Silverline | 7X Mulesoft Certified | 2X Salesforce Certified | Jitterbit Certified
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.
You can see in detail : - click here
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.
You can see in detail : - click here