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
Since the release of Android Oreo and its support library (26.0.0) you can do this easily. Refer to this answer in another question.
Basically your final style will look like this:
Tom's solution works great, but only works with TextView and EditText.
If you want to cover most of the views (RadioGroup, TextView, Checkbox...), I created a method doing that :
This method will get back the style of the view set in XML (bold, italic...) and apply them if they exists.
For the ListView, I always create an adapter, and I set the font inside getView.
I would also like to improve weston's answer for API 21 Android 5.0.
I had the same issue on my Samsung s5, when using DEFAULT font. (with the others fonts it's working fine)
I managed to make it working by setting the typeface ("sans" for example) in XML files, for each Textview or Button
and in MyApplication Class :
Hope it helps.
There is a great library for custom fonts in android:Calligraphy
here is a sample how to use it.
in Gradle you need to put this line into your app's build.gradle file:
and then make a class that extends
Application
and write this code:and in the activity class put this method before onCreate:
and the last thing your manifest file should look like this:
and it will change the whole activity to your font! it's simple and clean!
in api 26 with build.gradle 3.0.0 and higher you can create a font directory in res and use this line in your style
for change build.gradle use this in your build.gradle dependecies