Avoiding fake fonts in Android

I recently encountered the problem of fake bold and italic text when using font family in Android development.



In this article I want to talk about this problem and its solution.



Creating a font family



Starting with API 26, it became possible to combine fonts into families.

A font family is a collection of font files with their style and weight.



You can create a new font family as an XML resource and refer to it as a single element, instead of referring to each style and weight as separate resources.



This way the system will be able to select the correct font based on the text style you are trying to use.



Example file:



<?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>


Option for Support Library
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font
        app:fontStyle="normal"
        app:fontWeight="400"
        app:font="@font/lobster_regular" />
    <font
        app:fontStyle="italic"
        app:fontWeight="400"
        app:font="@font/lobster_italic" />
</font-family>


The attribute fontStyledetermines the style of the font - regular ( normal) or italic ( italic).

In turn, fontWeight- sets the weight, aka the weight of the font.

And of course, it fontwill set the font that will be used for the given fontWeightand fontStyle.



Font weight



This standard came from web development. The value is set from 100 to 900 in increments of 100.



The following table corresponds to common names for saturation:



Value Common name
100 ()
200
300
400
500
600
700
800
900 ()


, , — 400, — 700.



.





, Android , .



, .



Lobster Two :







, , . , - .





, lobster_two.xml , , , :



<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font
        app:fontStyle="normal"
        app:fontWeight="400"
        app:font="@font/lobster_two_normal" />
    <font
        app:fontStyle="italic"
        app:fontWeight="400"
        app:font="@font/lobster_two_italic" />
    <font
        app:fontStyle="normal"
        app:fontWeight="700"
        app:font="@font/lobster_two_bold" />
    <font
        app:fontStyle="italic"
        app:fontWeight="700"
        app:font="@font/lobster_two_bold_italic" />
</font-family>


, lobster_two_incomplete.xml :



<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font
        app:fontStyle="normal"
        app:fontWeight="400"
        app:font="@font/lobster_two_normal" />
</font-family>


.



lobster_two_incomplete.xml, lobster_two.xml, .



, , .





// 
val typeFace = resources.getFont(R.font.lobster_two)
textView.setTypeface(typeFace, Typeface.BOLD)

//  
textView.typeface = resources.getFont(R.font.lobster_two_bold)

//  
val typeFace = resources.getFont(R.font.lobster_two_incomplete)
textView.setTypeface(typeFace, Typeface.BOLD)

//  
val typeFace = resources.getFont(R.font.lobster_two_normal)
textView.setTypeface(typeFace, Typeface.BOLD)


xml


// 
<TextView
          ...
          android:fontFamily="@font/lobster_two"
          android:textStyle="bold|italic"/>

//  
<TextView
          ...
          android:fontFamily="@font/lobster_two_bold_italic"/>

//  
<TextView
          ...
          android:fontFamily="@font/lobster_two_incomplete"
          android:textStyle="bold|italic"/>

// 
<TextView
          ...
          android:fontFamily="@font/lobster_two"
          android:textStyle="bold"/>

//  
<TextView
          ...
          android:fontFamily="@font/lobster_two_bold"/>

//  
<TextView
          ...
          android:fontFamily="@font/lobster_two_normal"
          android:textStyle="bold"/>



All Articles