“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Android uses custom fonts

Sometimes interface design app will use some beautiful fonts, it is not difficult to use in Android, with the update of Android SDK, it is implemented in a different way, this article to see how to achieve custom fonts.

Common methods

Set the font. TextView.setTypeface(Typeface)

 public void setTypeface(@Nullable Typeface tf) {
 }
Copy the code

So we can just create a Typeface, and then we can set up our TextView and Typeface will do it.

Requirement scenario is like this, we download a lot of fonts from the server, let the user to choose the font, so in the font selection interface, also want to preview the font.

Let’s start with a rendering

All we need to do is download the font and set the font in the adapter. Take a look at the code in the adapter:

public class FontAdapter extends BaseRecyclerViewAdapter<FontInfo> { public FontAdapter(@Nullable List<FontInfo> data) {  super(R.layout.item_font, data); } @Override public void convert(BaseViewHolder helper, FontInfo item, TextView TextView = helper.getView(r.i.i.tem_font_name); textView.setText(item.getName()); String fontPath = FontManager.getFontPathByCoding(item.getId()); Typeface typeface = Typeface.createFromFile(fontPath); if (typeface ! = null) textView.setTypeface(typeface); }}Copy the code

This code is not very complicated, download from the server to the local code has been omitted, what we need to do is to obtain the local font path, and then display, here must pay attention to the typeface non-empty judgment, otherwise the program will flash back.

Fonts in XML

Fonts in XML is a feature available in Android 8.0. This means that Font resources can be used like r.string.xx and Fonts can be used like any other resource file, making it easier to replace global Fonts. To use it, just @font/myfont or r.font. Myfont will do. Let’s see how it works.

1. Create the font directory

Right-click the res folder and choose New > Android Resource Directory. Then select Font from resource Type. The directory name must also be font. Refer to the figure below.

2. Add fonts and references

Copy one or more font files to font folder. The file format supports TTF and OTF. We can use it via @font/myfont or r.font. Myfont. We can also create a style in the style file for uniformity and reference directly in the XML of the configuration file.

  <style name="FontStyle" parent="AppTheme">
        <item name="android:fontFamily">@font/font_1</item>
    </style>
Copy the code

3. Use fonts

How do you use fonts?

<activity
            android:name=".MainActivity"
            android:theme="@style/FontStyle">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Copy the code

It can also be used globally

 <application
        android:theme="@style/FontStyle">
 </application>
Copy the code