A
A
Anatoly Sergeevich2017-05-28 11:41:14
Android
Anatoly Sergeevich, 2017-05-28 11:41:14

Android connection Fonts?

Good afternoon, guys, how to connect fonts to the project, globally.
For example, connected in one place so that they apply to everyone?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2017-05-28
@NubSteel

public class FontChangeCrawler {
    private Typeface typeface;

    public FontChangeCrawler(Typeface typeface) {
        this.typeface = typeface;
    }

    public FontChangeCrawler(AssetManager assets, String assetsFontFileName) {
        typeface = Typeface.createFromAsset(assets, assetsFontFileName);
    }

    public void replaceFonts(ViewGroup viewTree) {
        View child;
        for (int i = 0; i < viewTree.getChildCount(); ++i) {
            child = viewTree.getChildAt(i);
            if (child instanceof ViewGroup) {
                replaceFonts((ViewGroup)child);
            }
            else if (child instanceof TextView) {
                ((TextView) child).setTypeface(typeface);
            }
        }
    }
}

public class MainActivity extends AppCompatActivity {
    @Override
    public void setContentView(View view) {
        super.setContentView(view);

        FontChangeCrawler fontChanger = new FontChangeCrawler(getAssets(), "fonts/myfont.otf");
        fontChanger.replaceFonts((ViewGroup)this.findViewById(android.R.id.content));
    }
}

D
Denis Zagaevsky, 2017-05-28
@zagayevskiy

Wait for Android O and support library 26.0.0 (beta1 is available), it is there in its normal form.
Or use the Calligraphy library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question