29

I'm trying to create custom font family using this doc https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html The problem in that that there is no bold attribute and I can not set it programmatically Fonts in XML?

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
Near1999
  • 1,547
  • 2
  • 18
  • 37

3 Answers3

38

Set 700 as the fontWeight value for your font in bold.

As it's in the font resources documentation:

The most common values are 400 for regular weight and 700 for bold weight.

In the lobster font family example you linked it would be:

<!-- bold -->
<font
    android:fontStyle="normal"
    android:fontWeight="700"
    android:font="@font/lobster_bold" />

And, if you are using support lib:

<!-- bold -->
<font
    app:font="@font/lobster_bold"
    app:fontStyle="normal"
    app:fontWeight="700" />
jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • 2
    so I need to create two font families? For normal and bold? – Near1999 Jan 15 '18 at 16:29
  • No, it's one family, you have the element defining the normal, another element defining the italic font and now you should add the bold font also as the other ones (3 elements inside the same ). – jeprubio Jan 15 '18 at 16:33
  • 6
    Note that the fontWeight="700" is what is defining the bold style and not the fontStyle – jeprubio Jan 15 '18 at 16:44
  • 1
    Is it still necessary to add both the `app` and `android` prefix? I tried it with only `app` on API 26 and 28 and it still worked. – Florian Walther Apr 14 '19 at 18:20
  • 2
    Well, it's been a while since I used this code but I've checked it and according to this doc https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml with the support library only the `app` namespace is needed. I've just edited the answer. You were right, thanks @FlorianWalther – jeprubio Apr 15 '19 at 07:52
2

In addition to @jeprubio answer, I should add this that if you are using one font family resource for different font style

for example you got serif_normal.ttf & serif_bold.ttf fonts and have one font family resource named serif_font.xml like this:

<?xml version="1.0" encoding="utf-8"?>

<font
    android:font="@font/serif_normal"
    android:fontStyle="normal"
    android:fontWeight="400"
    app:font="@font/serif_normal"
    app:fontStyle="normal"
    app:fontWeight="400" />

<font
    android:font="@font/serif_bold"
    android:fontStyle="normal"
    android:fontWeight="700"
    app:font="@font/serif_bold"
    app:fontStyle="normal"
    app:fontWeight="700" />

for referring to the bold weight or normal you can use

android:textStyle="normal | bold"

or you can create a custom style like this :

<style name="TextAppearance.BoldFond" parent="android:TextAppearance">
    <item name="android:fontFamily">@font/sarif_font</item>
    <item name="fontFamily">@font/sarif_font</item>
    <item name="android:textStyle">bold</item>
    //another attributes
</style>

checkout Here for full description.

Sepehr
  • 960
  • 11
  • 17
1

Download Roboto Font family from Google Robot Font URL

extract the folder and get Roboto-Bold.ttf file and paste this font file in res>font folder.

now you can use this font in xml like this way

<TextView
   android:layout_width="wrap_content" 
android:layout_height="wrap_content"
     android:fontFamily="@font/robot_bold" />

You can also set a simple font and set android:textStyle="bold" to bold the text

scienticious
  • 438
  • 1
  • 7
  • 22