Apply font on TextView by xml (Not required Java code)
attrs.xml: (Where to place res/values)
How to use:
Custom font in canvas text
Drawing text in canvas with your font from assets.
Custom font to whole activity
Then in your activity, in onCreate() method:
Efficient Typeface loading
Loading custom fonts can be lead to a bad performance. I highly recommend to use this little helper which saves/loads your already used fonts into a Hashtable.
Initializing a font
Putting a custom font in your app
Go to the (project folder)
Then app -> src -> main.
Create folder 'assets -> fonts' into the main folder.
Put your 'fontfile.ttf' into the fonts folder.
Using a custom font in a TextView
Working with fonts in Android O
Android O changes the way to work with fonts.
Android O introduces a new feature, called Fonts in XML, which allows you to use fonts as resources. This means, that there is no need to bundle fonts as assets. Fonts are now compiled in an R file and are automatically available in the system as a resource.
In order to add a new font, you have to do the following:
Create a new resource directory: res/font.
Add your font files into this font folder. For example, by adding myfont.ttf, you will be able to use this font via R.font.myfont.
You can also create your own font family by adding the following XML file into the res/font directory:
You can use both the font file and the font family file in the same way:
In an XML file, by using the android:fontFamily attribute, for example like this:
Or like this:
In your code, by using the following lines of code:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0