In Android, the three default font
options are monospace,
sans serif and serif. You do have some flexibility with adding effects to the
default fonts, but without custom fonts, you are pretty limited in how text in
your Android appears to your users. Luckily, it’s fairly straightforward to set
a custom font. You can find plenty of free custom fonts at sites likedafont.com – just download the .ttf file
for the font of your choice, and add it to the assets/fonts directory of your
codebase (create the directory if it doesn’t exist already).
Custom Fonts in
Views
If, for example, you’d like to set
the font for a TextView with id=name, it’s as simple as adding the following to
your code:
TextView name = (TextView) findViewById(R.id.name);
Typeface typeface = Typeface.createFromAsset(getContext().getAssets()"fonts/customFont.ttf");
name.setTypeface(typeface);
Typeface typeface = Typeface.createFromAsset(getContext().getAssets()"fonts/customFont.ttf");
name.setTypeface(typeface);
That’s fine when you have just one or two TextViews with custom fonts,
but it gets to be a pain when you want customized fonts on all your TextViews.
That’s a lot of boilerplate code to have in your activities! It would be
convenient to be able to specify the font name in xml. To do this, you can
extend TextView:
public class CustomTextView extends TextView {
private static final DEFAULT_FONT="defaultFont.ttf";
public CustomTextView(Context context) {
this(context, null);
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);
String fontName = arr.getString(R.styleable.CustomTextView_fontName);
// Sets to a default font if none is specified in xml
if (fontName == null) {
fontName = DEFAULT_FONT;
}
setFont(fontName);
}
private void setFont(String fontName) {
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
setTypeface(typeface);
}
}
private static final DEFAULT_FONT="defaultFont.ttf";
public CustomTextView(Context context) {
this(context, null);
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);
String fontName = arr.getString(R.styleable.CustomTextView_fontName);
// Sets to a default font if none is specified in xml
if (fontName == null) {
fontName = DEFAULT_FONT;
}
setFont(fontName);
}
private void setFont(String fontName) {
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
setTypeface(typeface);
}
}
In your attrs.xml (in res/values/, create the xml file if it doesn’t
exist already), add an attribute for CustomTextView as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomTextView">
<attr name="fontName" format="string"/>
</declare-styleable>
</resources>
<resources>
<declare-styleable name="CustomTextView">
<attr name="fontName" format="string"/>
</declare-styleable>
</resources>
Now you can add a custom font to a TextView in xml, without any
additional code to your activities:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:example="http://schemas.android.com/apk/res/com.example.customfonts"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.customfonts.CustomTextView
example:fontName="customFont2.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:example="http://schemas.android.com/apk/res/com.example.customfonts"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.customfonts.CustomTextView
example:fontName="customFont2.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
That’s it! Now you can easily set
custom fonts for views in xml.
Custom Fonts in
WebViews
For webviews, you still want to copy
the .ttf file into your assets/fonts directory, and then you can create new
font-faces in your css and use them by setting font-family:
<style type="text/css">
@font-face {
font-family:"customFont1";
src:url('file:///android_asset/fonts/customFont.ttf');
}
@font-face {
font-family:"PTS55F";
src:url('file:///android_asset/fonts/customFont2.ttf');
}
body { font-family:'customFont'; }
h1 { font-family:'customFont2'; }
</style>
@font-face {
font-family:"customFont1";
src:url('file:///android_asset/fonts/customFont.ttf');
}
@font-face {
font-family:"PTS55F";
src:url('file:///android_asset/fonts/customFont2.ttf');
}
body { font-family:'customFont'; }
h1 { font-family:'customFont2'; }
</style>
Using custom fonts gives you a lot more control over the visual
appearance of your application, but when don’t get carried away — using too
many random fonts can make an app seem very disjointed. In general, try to use
at most a few fonts that go well together. To get you started, here are 40 free designer
fonts.
regarding the second part of this post, "custom fonts in webviews"
ReplyDeleteIs it applicable for Android OS 4.1 ? it seems that it doesn't work for the newest android os.
This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
ReplyDeleteAndroid Training in chennai | Android Training
Not working for me
ReplyDeleteI am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.
ReplyDeleteClick here:
angularjs training in tambaram
Click here:
angularjs6 Training in Chennai
I likable the posts and offbeat format you've got here! I’d wish many thanks for sharing your expertise and also the time it took to post!!
ReplyDeleteClick here:
Microsoft azure training in annanagar
Click here:
Microsoft azure training in velarchery
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
ReplyDeleteDevops training in sholinganallur
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeletebest rpa training in chennai |
rpa training in chennai |
rpa training in bangalore
rpa training in pune | rpa online training
Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...
ReplyDeleteData Science course in rajaji nagar | Data Science with Python course in chenni
Data Science course in electronic city | Data Science course in USA
Data science course in pune | Data science course in kalyan nagar
Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
ReplyDeletejava training in tambaram | java training in velachery
java training in omr | oracle training in chennai
Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
I inspired fro your post ethical hacking online course
ReplyDeletenice post...!
ReplyDeleteinternship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai
It was really a wonderful article and I was really impressed by reading this blog. We are giving all software and Database Course Online Training.thnks aa lot guys.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
your efforts and work make me realize that anything can be done with patience and sincerity. thank you so much for your article.
ReplyDeletePython Training in Chennai | Certification | Online Training Course | Python Training in Bangalore | Certification | Online Training Course | Python Training in Hyderabad | Certification | Online Training Course | Python Training in Coimbatore | Certification | Online Training Course | Python Training in Online | Python Certification Training Course
I am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.
ReplyDeletejava training in chennai
java training in omr
aws training in chennai
aws training in omr
python training in chennai
python training in omr
selenium training in chennai
selenium training in omr
Marvelous Blog.. you are a great author. I will make sure to bookmark your blog and may come back daily so kindly keep updating such a great blog.
ReplyDeletejava training in chennai
java training in tambaram
aws training in chennai
aws training in tambaram
python training in chennai
python training in tambaram
selenium training in chennai
selenium training in tambaram
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve
ReplyDeletemy knowledge as updated one, keep blogging\
java training in chennai
java training in porur
aws training in chennai
aws training in porur
python training in chennai
python training in porur
selenium training in chennai
selenium training in porur
Well I am here to say that your work has inspired me without a doubt.
ReplyDeletejava training in chennai
java training in annanagar
aws training in chennai
aws training in annanagar
python training in chennai
python training in annanagar
selenium training in chennai
selenium training in annanagar
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve
ReplyDeletemy knowledge as updated one, keep blogging\salesforce training in chennai
software testing training in chennai
robotic process automation rpa training in chennai
blockchain training in chennai
devops training in chennai
En son çıkan perde modelleri
ReplyDeleteMobil onay
Vodafone Mobil Ödeme Bozdurma
nft nasıl alınır
ankara evden eve nakliyat
Trafik sigortasi
DEDEKTOR
Web sitesi kurmak
Ask kitaplari
smm panel
ReplyDeletesmm panel
iş ilanları
instagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
SERVİS
tiktok hile