Fonts for Android

Good afternoon! In this article I want to discuss such an important issue as fonts in Android. We will create our own TextView with the ability to add fonts to xml and visually display them in a preview. We’ll also solve one important problem - the use of fonts in the list, without glitches and strains for our smaller brothers, our Android devices.

Next, I will describe how to create the assets folder and add my fonts, so you can skip this.

Creating the assets folder and adding fonts
The assets folder is needed to store a wide variety of resources, including fonts. You can create it either manually in the root of main:
\ app \ src \ main \ assets
Or in a simpler way
image

Further we drop the files with the .ttf format into the assets either in the root or create the fonts folder, since assets support nesting.

So, now let's turn to the implementation of the use of fonts for TextView, in its pure form it looks approximately as follows:

  Typeface type = Typeface.createFromAsset(getAssets(),"fonts/font1.ttf"); 
  myTextView.setTypeface(type);

When it comes to more than five TextViews, this process starts to get annoying. But annoyance is not the only problem encountered by a developer using this design. The fact is that the processing of the font file itself and turning it into a Typeface object is a rather time-consuming process, therefore, the use of such a design in the sheet will cause incredible glitches.

I propose to solve this problem with a regular singleton. And so we create something similar:

  
public class SingletonFonts {
    private static Typeface font1;
    private static Typeface font2;
    private static Typeface font3;
    public Typeface getFont1() {
        return font1;
    }
    public  Typeface getFont2() {
        return font2;
    }
    public Typeface getFont3() {
        return font3;
    }
    public static void setFont1(Typeface font1) {
        SingletonFonts.font1 = font1;
    }
    public static void setFont2(Typeface font2) {
        SingletonFonts.font2 = font2;
    }
    public static void setFont3(Typeface font3) {
        SingletonFonts.font3 = font3;
    }
    private static volatile SingletonFonts instance;
    private SingletonFonts() {}
    public static SingletonFonts getInstance(Context activity) {
        SingletonFonts localInstance = instance;
        if (localInstance == null) {
            synchronized (SingletonFonts.class) {
                localInstance = instance;
                if (localInstance == null) {
                    instance = localInstance = new SingletonFonts();
                }
            }
            setFont1(Typeface.createFromAsset(activity.getAssets(), "fonts/font1.ttf"));
            setFont2(Typeface.createFromAsset(activity.getAssets(), "fonts/font2.ttf"));
            setFont3(Typeface.createFromAsset(activity.getAssets(), "fonts/font3.ttf"));
        }
        return localInstance;
    }
}

And install the fonts using singleton, like this:

public class MainActivity extends AppCompatActivity {
    TextView textView1;
    TextView textView2;
    TextView textView3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1 = (TextView) findViewById(R.id.text_view_1);
        textView2 = (TextView) findViewById(R.id.text_view_2);
        textView3 = (TextView) findViewById(R.id.text_view_3);
        textView1.setTypeface(SingletonFonts.getInstance(this).getFont1());
        textView2.setTypeface(SingletonFonts.getInstance(this).getFont2());
        textView3.setTypeface(SingletonFonts.getInstance(this).getFont3());
    }
}

Using this design, we solved the second problem, regarding lags in the sheet, but not the first, most important one, you still need to find all the text fields and set fonts over and over again, write individual methods and lose nerve cells. Another problem is that in preview, the standard font, therefore the width, height of its font. You can only see on a live device or emulator, it is very not convenient.

Introducing the above problems, we will now write our TextView with blackjack and fonts.

First of all, we create a class that inherits from a regular TextView with constructors:

public class CustomFontsTextView extends TextView {
    public CustomFontsTextView(Context context) {
        super(context);
    }
    public CustomFontsTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomFontsTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public CustomFontsTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}

Next, create the attrs.xml file in the values ​​folder

Creating attrs.xml
We go here

image

and create a file called attrs.xml, after creation it should look something like this:



We create the following code block in it:

  //имя вашего класса 
    
        // перечень всех ваших шрифтов. name = "fonts" - название xml атрибута
        

Next, we return to our CustomFontsTextView class and write this method:

   public class CustomFontsTextView extends TextView {
    public CustomFontsTextView(Context context) {
        super(context);
    }
    public CustomFontsTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFonts(attrs,context);
    }
    public CustomFontsTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFonts(attrs,context);
    }
    public CustomFontsTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setFonts(attrs,context);
    }
    private void setFonts(AttributeSet attributeSet, Context context){
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attributeSet,
                R.styleable.CustomFontsTextView,
                0, 0);
        a.recycle();
        int fonts = a.getInt(R.styleable.CustomFontsTextView_fonts,-1);
        SingletonFonts singltonFonts = SingletonFonts.getInstance(context);
        switch (fonts){
            case (0):
                setTypeface(singltonFonts.getFont1());
                break;
            case (1):
                setTypeface(singltonFonts.getFont2());
                break;
            case (2):
                setTypeface(singltonFonts.getFont3());
                break;
        }
    }
}

That's all. Now you need to rebuild the project so that you have custom attributes. After that, we go to the xml file of our activity and write:


In the preview, we will see fully finished text fields with our fonts.

image
It is better to access our custom text fields like a regular TextView, in a standard form:

 TextView textView = (TextView) findViewById(R.id.text_view_1);

Thanks for attention.

Also popular now: