So I'd like to change the android:fontFamily
in Android but I don't see any pre-defined fonts in Android. How do I select one of the pre-defined ones? I don't really need to define my own TypeFace but all I need is something different from what it shows right now.
<TextView
android:id="@+id/HeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:gravity="center"
android:text="CallerBlocker"
android:textSize="40dp"
android:fontFamily="Arial"
/>
It seems what I did up there won't really work! BTW android:fontFamily="Arial"
was a stupid attempt!
The valid value of android:fontFamily is defined in /system/etc/system_fonts.xml(4.x) or /system/etc/fonts.xml(5.x). But Device Manufacturer might modify it, so the actual font used by setting fontFamily value depends on the above-mentioned file of the specified device.
In AOSP, the Arial font is valid but must be defined using "arial" not "Arial", for example android:fontFamily="arial". Have a qucik look at Kitkat's system_fonts.xml
//////////////////////////////////////////////////////////////////////////
There are three relevant xml-attributes for defining a "font" in layout--android:fontFamily, android:typeface and android:textStyle. The combination of "fontFamily" and "textStyle" or "typeface" and "textStyle" can be used to change the appearance of font in text, so does used alone. Code snippet in TextView.java like this:
From the code We can see:
Here is an easier way that can work in some cases. The principle is to add a not visible TextVview in your xml layout and to get its typeFace in the java code.
The layout in the xml file:
And the java code:
It has worked for me (within a TextSwitcher for example).
try these simple steps. 1. create font folder in res folder. 2. copy and paste .ttf file into font folder. 3. Now give the path in xml like below.
or what ever your file name is. Thats it happy code
Dynamically you can set the fontfamily similar to android:fontFamily in xml by using this,
These are the list of default font family used, use any of this by replacing the double quotation string "sans-serif-medium"
"mycustomfont.ttf" is the ttf file. Path will be in src/assets/fonts/mycustomfont.ttf , you can refer more about default font in this Default font family
Starting from Android-Studio 3.0 its very easy to change font family
Using support library 26, it will work on devices running Android API version 16 and higher
Create a folder
font
underres
directory .Download the font which ever you want and paste it insidefont
folder. The structure should be some thing like belownow you can change font in layout using
To change Programatically
To change font using styles.xml create a style
and apply this style to
TextView
you can also Create your own font family
- 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.
Write your own font family here , for example
this is simply a mapping of a specific fontStyle and fontWeight to the font resource which will be used to render that specific variant. Valid values for fontStyle are normal or italic; and fontWeight conforms to the CSS font-weight specification
1. To change fontfamily in layout you can write
2. To Change Programmatically
Note: As of Android Support Library 26.0, you must declare both sets of attributes ( android: and app: ) to ensure your fonts load on devices running Api 26 or lower.
To change font of entire App Add these two lines in AppTheme
See the Documentation , Android Custom Fonts Tutorial For more info
This is also a good library RobotoTextView. It really serves your needs.