How to change fontFamily of TextView in Android
From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black" // roboto black
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
in combination with
android:textStyle="normal|bold|italic"
this 16 variants are possible:
- Roboto regular
- Roboto italic
- Roboto bold
- Roboto bold italic
- Roboto-Light
- Roboto-Light italic
- Roboto-Thin
- Roboto-Thin italic
- Roboto-Condensed
- Roboto-Condensed italic
- Roboto-Condensed bold
- Roboto-Condensed bold italic
- Roboto-Black
- Roboto-Black italic
- Roboto-Medium
- Roboto-Medium italic
fonts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>
Set font at runtime, TextView
To set In-built Font at Run-Time:
First of all, To Change Font-face, a Typeface class is used.
Now,
at Run-Time
, to set the font-face, UsesetTypeface(Typeface)
from the Java codeat Design-Time
, to set the font-face, Useandroid:typeface="serif"
For example:
<TextView android:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:textStyle="italic"
android:typeface="serif" />
To set Custom font(s) in your Android application
To do this, simply create an assets/ folder in the project root, and put your fonts (in TrueType, or TTF, form) in the assets. You might, for example, create assets/fonts/
and put your TTF files in there:
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face);
Android Studio Set font family on Textview
You have to put the font's file (galada.ttf in your own case) in the /res/font folder, then use android:fontFamily="@font/galada"
in your TextView.
OR
put the font's file in /assets/fonts folder and use this code in your java file to set its typeface.
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/galada.ttf");
textview.setTypeface(typeface);
How to change the font on the TextView?
First, the default is not Arial. The default is Droid Sans.
Second, to change to a different built-in font, use android:typeface
in layout XML or setTypeface()
in Java.
Third, there is no Helvetica font in Android. The built-in choices are Droid Sans (sans
), Droid Sans Mono (monospace
), and Droid Serif (serif
). While you can bundle your own fonts with your application and use them via setTypeface()
, bear in mind that font files are big and, in some cases, require licensing agreements (e.g., Helvetica, a Linotype font).
EDIT
The Android design language relies on traditional typographic tools
such as scale, space, rhythm, and alignment with an underlying grid.
Successful deployment of these tools is essential to help users
quickly understand a screen of information. To support such use of
typography, Ice Cream Sandwich introduced a new type family named
Roboto, created specifically for the requirements of UI and
high-resolution screens.The current TextView framework offers Roboto in thin, light, regular
and bold weights, along with an italic style for each weight. The
framework also offers the Roboto Condensed variant in regular and bold
weights, along with an italic style for each weight.
After ICS, android includes Roboto fonts style,
Read more Roboto
EDIT 2
With the advent of Support Library 26, Android now supports custom fonts by
default. You can insert new fonts in res/fonts which can be set to TextViews individually either in XML or programmatically. The default font for the whole application can also be changed by defining it styles.xml The android developer documentation has a clear guide on this here
Set font for all textViews in activity?
Solution1:: Just call these method by passing parent view as argument.
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView ) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
}
} catch (Exception e) {
}
}
Solution2:: you can subclass the TextView class with your custom font and use it instead of textview.
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
setTypeface(tf);
}
}
}
Related Topics
Android Camera: Onactivityresult() Intent Is Null If It Had Extras
Displaying Android Asset Files in a Webview
Difference Between Android: and App: Prefix in Android Xml
Android: How to Measure Total Height of Listview
Android Support Library Error After Updating to 23.3.0
Start Google Hangout from Intent in New Hangouts Android App
Searchview in Optionsmenu Not Full Width
Stop Fragment Refresh in Bottom Nav Using Navhost
How to Merge Two Mp3 Files into One (Combine/Join)
Firebase Authentication Not Working in Signed APK
How to Check the Internet Connection Periodically in Whole Application
Android:Dither="True" Does Not Dither, What's Wrong
Developing Two Android Apps and Communicating Between Two
How to Limit Fling in Android Gallery to Just One Item Per Fling
Soft Keyboard Shows Up on Edittext Focus Only Once
Android - Getting from a Uri to an Inputstream to a Byte Array