Set font at runtime, TextView
Solution 1
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 code-
at 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);
Solution 2
You can have .ttf font in your asset folder. Say font's name is "default.ttf" and you just now have to write below 2 lines of code
TextView text = new TextView(this);
text.setTypeface(Typeface.createFromAsset(getAssets(), "default.ttf"));
You must also we careful because different font have different sizes. You may need to set size as :
text.setTextSize(20);
Solution 3
you can use your font which you have store in font "res/font" ex. for API level 16 and above.
Typeface typeface = ResourcesCompat.getFont(context, R.font.rubik_medium);
txtView.setTypeface(typeface);
you can also use
Typeface typeface = getResources().getFont(R.font.rubik_medium);
txtView.setTypeface(typeface);
but it support with API level 26 and above.
Solution 4
With introduction of Fonts in XML in Android 8.0 (backward compatible from API version 14) its very easy to set font from xml itself.
From the android documentation:
Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio. You can access the font resources with the help of a new resource type, font. For example, to access a font resource, use @font/myfont, or R.font.myfont.
Firstly create a Android Resource Directory in res
folder named as font
Add your .ttf font file to that directory, and then create font family
Create a font family
A font family is a set of font files along with its style and weight details. In Android, you can create a new font family as an XML resource and access it as a single unit, instead of referencing each style and weight as separate resources. By doing this, the system can select the correct font based on the text style you are trying to use.
To create a font family, perform the following steps in the Android Studio:
- Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
- Enter the file name, and then click OK. The new font resource XML opens in the editor.
-
Enclose each font file, style, and weight attribute in the
<font>
element. The following XML illustrates adding font-related attributes in the font resource XML:<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/lobster_regular" /> <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/lobster_italic" /> </font-family>
Then use the following code to set font in your textView
like
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>
Solution 5
Here is a small utility class
public class TypefaceHelper {
public static void setViewGroupTypeface(ViewGroup container, Typeface typeface) {
final int children = container.getChildCount();
for (int i = 0; i < children; i++)
View child = container.getChildAt(i);
if (child instanceof TextView) {
setTextViewTypeface((TextView) child, typeface);
} else if (child instanceof ViewGroup) {
setViewGroupTypeface((ViewGroup) child, typeface);
}
}
}
public static void setTextViewTypeface(TextView textView, Typeface typeface) {
textView.setTypeface(typeface);
}
}
For things like Spinner
s or ListView
s (i.e. any kind of AdapterView
) which generate their children from an adapter you will need to set the typeface of each item View
in the getView
(or similar) method of the adapter. This is because views may be created as needed and so setting the Typeface
in onCreate
won't work properly.
Shishir.bobby
From Java to Android to iOS App Developer to Project Manager(now).
Updated on July 09, 2022Comments
-
Shishir.bobby almost 2 years
How to set the font of a TextView created at runtime?
I created a TextView
Textview tv = new TextView(this); tv.setTextSize(20);
I can easily change the size, now I'd like to set font style to "Verdana".
How to do this?
-
Nikunj Patel almost 13 yearsone mistake i have found in your example is that font extention not small letter but in capital letter like "fonts/HandmadeTypewriter.TTF"
-
Bill Gary over 12 yearsthe font extention can be either caps TTF or lower case ttf, it just has to match the way it is in your asset folder
-
Joseph Earl over 11 yearsRather than using reflection you should check whether the child is an instance of
TextView
orViewGroup
-
LuminiousAndroid over 11 yearsSimply Use this :- TextView tv=(TextView)findViewById(R.id.custom); Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"); tv.setTypeface(face);
-
Muzikant over 11 years@JosephEarl @Avin Checking if it's a
TextView
is not sufficient since you also want to set the font on buttons, radio groups, spinners... (In fact, any control that supports thesetTypeface
method) - And this is why using reflection is the most general way to do that. If you only wish to set fonts ofTextView
, you can avoid it but otherwise you'll need to check each and every widget type and cast accordingly. -
Joseph Earl over 11 years@Muzikant
Spinner
extendsViewGroup
, andButton
extendsTextView
so as I said checking for it being an instance ofTextView
orViewGroup
is generally sufficient. -
gone over 7 years@NikunjPatel and the extension can also be completely removed.