Set font at runtime, TextView

Viewed 62014

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?

9 Answers

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.

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:

  1. Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
  2. Enter the file name, and then click OK. The new font resource XML opens in the editor.
  3. 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"/>

If you don't want to use typeface and fiddle around with the path to your font file (Which may crash your application if you put in an incorrect path), here's another way.

First create a style in your styles.xml

<style name="YourFont">
    <item name="android:fontFamily">@font/your_font</item>
</style>

Then you can just add the style using

textView.setTextAppearance(context, R.style.YourFont);
Related