How to set font to textview, which is created at runtime?
i created textview
Textview tv = new TextView(this);
tv.setTextSize(20);
like I can change the size
i want to set font style to "Verdana".
How to do this?? regards shishir
How to set font to textview, which is created at runtime?
i created textview
Textview tv = new TextView(this);
tv.setTextSize(20);
like I can change the size
i want to set font style to "Verdana".
How to do this?? regards shishir
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:
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: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
You must also we careful because different font have different sizes. You may need to set size as :
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:
Firstly create a Android Resource Directory in
res
folder named as fontAdd 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:
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
likeYou can use the following code to set all your text to a specific font at runtime. Just call the
setViewGroupFont
method at the end of your ActivityonCreate
method or whenever you dynamically create new views:You need to use Typeface:
create Typeface object using that font:
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/MyFont.ttf");
set typeface to the object you'd like to customize:
TextView myTextView = (TextView)findViewById(R.id.my_text_view); myTextView.setTypeface(myFont);
Here is a small utility class
For things like
Spinner
s orListView
s (i.e. any kind ofAdapterView
) which generate their children from an adapter you will need to set the typeface of each itemView
in thegetView
(or similar) method of the adapter. This is because views may be created as needed and so setting theTypeface
inonCreate
won't work properly.