I need to use certain font for my entire application. I have .ttf file for the same. Is it possible to set this as default font, at application start up and then use it elsewhere in the application? When set, how do I use it in my layout XMLs?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
In summary:
Option#1: Use reflection to apply font (combining weston & Roger Huang's answer):
Usage in Application class:
set up a style to force that font typeface application wide (based on lovefish):
Pre-Lollipop:
Lollipop (API 21):
Option2: Subclass each and every View where you need to customize font, ie. ListView, EditTextView, Button, etc. (Palani's answer):
Option 3: Implement a View Crawler that traverses through the view hierarchy of your current screen:
Variation#1 (Tom's answer):
Usage :
Variation#2: https://coderwall.com/p/qxxmaa/android-use-a-custom-font-everywhere.
Option #4: Use 3rd Party Lib called Calligraphy.
Personally, I would recommend Option#4, as it saves a lot of headaches.
As of Android O this is now possible to define directly from the XML and my bug is now closed!
See here for details
TL;DR:
First you must add your fonts to the project
Second you add a font family, like so:
Finally, you can use the font in a layout or style:
Enjoy!
While this would not work for an entire application, it would work for an Activity and could be re-used for any other Activity. I've updated my code thanks to @FR073N to support other Views. I'm not sure about issues with
Buttons
,RadioGroups
, etc. because those classes all extendTextView
so they should work just fine. I added a boolean conditional for using reflection because it seems very hackish and might notably compromise performance.Note: as pointed out, this will not work for dynamic content! For that, it's possible to call this method with say an
onCreateView
orgetView
method, but requires additional effort.Then to use it you would do something like this:
Hope that helps.
Finally, Google realized the severity of this problem (applying custom font to UI components) and they devised a clean solution for it.
First, you need to update to support library 26+ (you may also need to update your gradle{4.0+}, android studio), then you can create a new resource folder called font. In this folder, you can put your font resources (.tff,...). Then you need to override the default app them and force your custom font into it :)
Note: if you want to support devices with older API than 16, you have to use app namespace instead of android!
A brilliant solution can be found here: https://coderwall.com/p/qxxmaa/android-use-a-custom-font-everywhere.
Simply extend activities from BaseActivity and write those methods. Also you should better cache fonts as described here: https://stackoverflow.com/a/16902532/2914140.
After some research I wrote code that works at Samsung Galaxy Tab A (Android 5.0). Used code of weston and Roger Huang as well as https://stackoverflow.com/a/33236102/2914140. Also tested on Lenovo TAB 2 A10-70L, where it doesn't work. I inserted a font 'Comic Sans' here in order to see a difference.
To run the code in entire application you should write in some class like Application the following:
Create a folder 'fonts' inside 'assets' and put needed fonts there. A simple instruction may be found here: https://stackoverflow.com/a/31697103/2914140.
The Lenovo device also incorrectly gets a value of a typeface. In most times it returns Typeface.NORMAL, sometimes null. Even if a TextView is bold (in xml-file layout). See here: TextView isBold always returns NORMAL. This way a text on a screen is always in a regural font, not bold or italic. So I think it's a bug of a producer.
its very simple... 1.Download and put ur custom font in assets..then write one separate class for text view as follows: here i used futura font
}
and do the following in xml :