I need to get all views that have text for setting custom fonts. I can develop a recursive method in myBaseActiviy class for getting all views with checking instanceof when programme is in OnCreate(). But I worry about the performance? I interest your idea? What should I do?
-
I use a recursive method myself, it is not that bad (I never observed a delay loading screens) – shalafi Jun 06 '14 at 11:37
-
@shalafi thanks for comment. I decided to use override default fonts like http://stackoverflow.com/a/16883281/2767703. But If I encounter a fault, i will use recursive method. – atasoyh Jun 10 '14 at 07:13
3 Answers
There is no best way. An approach, the one I use, is to subclass TextView
, adding a new attribute to specify the font I want to use, delegating the logic to the subclass self.

- 156,034
- 29
- 297
- 305
-
thaks for suggestion. But I need to set custom font for all texts, Edittext, Button, Spinner,... – atasoyh Jun 06 '14 at 10:44
I think the easiest way is to create your own TextView
. It's not as hard as it sounds ;-).
This is the origional answer:
https://stackoverflow.com/a/19679639/2767703
And this is the link you'll need:
http://javatechig.com/android/using-external-fonts-in-android-view
Or if you want to set the font in the xml:
https://stackoverflow.com/a/7197867/2767703
Warning
If you are developing for Android versions lower than Android 4.0 then you should change the code. In these versions there is a bug that doesn't free up memory for typefaces. What you should do is create a HashMap that allows reusage of Typefaces. You can find more in the comments of the last link (search for the comment with the highest upvote).
You could also use this:
https://stackoverflow.com/a/16883281/2767703
This changes the font of every text in your application. Note: You still have to look at my warning if you use this.

- 1
- 1

- 9,554
- 5
- 44
- 76
-
thank you, I was thinking about ApplyFont Method. But I really worry about performance... – atasoyh Jun 06 '14 at 10:45
-
1This link maybe even better: http://stackoverflow.com/a/7197867/2767703. It allows you to set the font in the xml. If you are developing for Android 4.0 and higher there shouldn't be a problem with performance. But if you are also developing for lower versions then you should be careful. Look in the comments (the one with 39 upvotes) on the link I gave you. You should create a HashMap that allows reusage of Typefaces. There was a bug before 4.0 that doesn't free up memory for typefaces. Hope this helps – Kevin van Mierlo Jun 06 '14 at 11:22
-
@atasoyh I realized you might want something different than my answer. Maybe this link will help you: http://stackoverflow.com/a/16883281/2767703. It sets every font for every text in your application. Of course you still have to be careful using the typefaces. – Kevin van Mierlo Jun 06 '14 at 11:42
You can do something like this:
- Add new attribute for store font in style.
- Extend your view to handle this attr and set font by view when do you need.
You can find example of using and creation of new attribute for font at this link https://stackoverflow.com/a/12282315/2156937
Hope it helps.