简体   繁体   中英

Easiest way to use custom fonts? - android

the way i found for using custom fonts on my android app is using the below code

TextView txt = (TextView) findViewById(R.id.textView1);  
        Typeface font = Typeface.createFromAsset(getAssets(), "ABCD.TTF");  
        txt.setTypeface(font);

i've stored the ABCD.TTF in assets folder..

and ya it works, no probs in tat.. the thing is i have to add the above code for each and every texts,buttons,etc i have.. and its really a time consuming thing if theres lots of texts and buttons in various activity :(

what i need is an alternate method to do it, an easiest one to do it..

like using a single block of code not repeatative like the above to change the fonts for all the stuffs..

else doing it in the xml

or is there any way to add our custom font to the inbuilt typeface where normal,sans,serif,monospace are present.

like using a single block of code not repeatative like the above to change the fonts for all the stuffs..

There are a few ways of doing this already addressed in various StackOverflow answers. Here is that iterates over the children of a ViewGroup and apply a Typeface to all that implement TextView :
- https://stackoverflow.com/a/7580370/115145

else doing it in the xml

Sorry, this is not supported.

or is there any way to add our custom font to the inbuilt typeface where normal,sans,serif,monospace are present.

Sorry, this is not supported, except if you build your own firmware.

You can use simple EasyFonts third party library to set variety of custom font to your TextView . By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.

Simply:

TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM