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!
If you want it programatically, you could use
Where
SANS_SERIF
you can use:DEFAULT
DEFAULT_BOLD
MONOSPACE
SANS_SERIF
SERIF
And where
ITALIC
you can use:BOLD
BOLD_ITALIC
ITALIC
NORMAL
All is stated on Android Developers
What you want is not possible. You must need to set
TypeFace
in your Code.In
XML
what you can do isother then this you can not play much with the Fonts in XML. :)
For
Arial
you need to set type face in your code.Android doesn't allow you to set custom fonts from the XML layout. Instead, you must bundle the specific font file in your app's assets folder, and set it programmatically. Something like:
Note that you can only run this code after setContentView() has been called. Also, only some fonts are supported by Android, and should be in a
.ttf (TrueType)
or.otf (OpenType)
format. Even then, some fonts may not work.This is a font that definitely works on Android, and you can use this to confirm that your code is working in case your font file isn't supported by Android.
Android O Update: This is now possible with XML in Android O, based on Roger's comment.
For android-studio 3 and above you can use this style and then all
textView
font change in app.create this style in your
style.xml
:Then use it in your theme :
An easy way to manage the fonts would be to declare them via resources, as such:
This is based on the source code here and here
This is the way to set the font programmatically:
put the font file in your assets folder. In my case I created a subdirectory called fonts.
EDIT: If you wonder where is your assets folder see this question