Using custom embedded fonts for a TextView

Go To StackoverFlow.com

0

So I have a layout that has a TextView:

<TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="GLASSES RX EXPIRATION DATE"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/black"
                android:typeface="monospace" />

I also have a futura font stored in my assets/fonts/ folder. I know how to set the typeface with java but is there a way right in the xml layout to set the font to my embeded asset font?

2012-04-04 23:32
by erik


0

No, there isn't a way to set a custom font in the xml layout beside the default ones, normal, monospace, sans and serif. You could make your own TextView, with a custom attribute that could take the extra font in xml and use that instead of the normal TextView class.

2012-04-05 05:38
by Luksprog


0

In the standard form, you will not be able to do it, but there is a way around it as mentioned by Luksprog. You basically need to

  1. Declare an attribute in a styleable which will be used to specify the font type.
  2. Add your custom fonts in the assets/fonts folder.
  3. Use the specified font type in the constructor of an extended TextView to access the custom fonts.

This will enable you to use your custom fonts without having to set it in java all over your application. Here's a tutorial for it.

2012-07-08 04:45
by Shubhayu
Ads