0

I have my own font that I want to use for my app in all layouts, I want to change the default font for my app.

In styles.xml I can use

 <item name="android:fontFamily"></item>

to change the change the font, but how do I use my custom font here?

In App -> src -> main, i made a new directory called Assets, then another directory in that one called Fonts, and this is where i placed my custom font.

Is this possible? If so, how?

EDIT

As mayosk said, I added

compile 'uk.co.chrisjenx:calligraphy:2.2.0'

And made a Java Class that extends Application as shown below

public class CustomResources extends Application {

@Override
public void onCreate() {
    super.onCreate();

    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
            .setDefaultFontPath("fonts/din_light.ttf")
            .setFontAttrId(R.attr.fontPath)
            .build()
    );
}
}

But the font is still the same, did I miss something

Community
  • 1
  • 1
Simon Andersson
  • 751
  • 1
  • 9
  • 29

3 Answers3

1

Use caligraphy : https://github.com/chrisjenx/Calligraphy

Add dependency to your app build.gradle:

compile 'uk.co.chrisjenx:calligraphy:2.2.0'

and extend your application class where you need to add this code to onCreate method

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("Fonts/customFont.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
        );

and also you need to override attachBaseContext method() in your activity:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
mayosk
  • 603
  • 3
  • 14
  • I edited my post, I tried this but didn't manage to fix it. Did I do something wrong? – Simon Andersson Mar 17 '17 at 09:10
  • i edited my answer, you need to override attachBaseContext method in your activity class where that font is used ... if you want to do that in all your activities make one BaseActivity class where you override this method and then extend others activities – mayosk Mar 17 '17 at 09:14
0

First of all you need to create a base class which extends Textview, here is the baseclass

public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setTypeFace(context);
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setTypeFace(context);
}

public MyTextView(Context context) {
    super(context);
    setTypeFace(context);
}

private void setTypeFace(Context context) {
    setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Bold.ttf"));
}}

later in xml

   <com.example.MyTextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="hi" />

same for edittext also. By doing this all the textviews and edittexts have same font entire the app.

Karthik
  • 1,199
  • 2
  • 14
  • 23
  • I could see how this would solve my problem, but then I would have to create custom views for all different views I use, right? However, if that's the case and mayosk's solution doesn't work, I will try this and see. – Simon Andersson Mar 17 '17 at 09:12
0

I have a special way to change the default font.I set a whole paint to create custom view and draw font on onDraw method,instead of xml way。 like this:

`private void initPaint(Context context)
{
    mTextPaint = new TextPaint();
    mTextPaint.setTextSize(66.46F);
    mTextPaint.setColor(Color.BLACK);

    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/MFKeKe_Noncommercial-Regular.ttf");
    mTextPaint.setTypeface(typeface);
    mTextPaint.setTextSkewX(-0.5F);

}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    mStaticLayout = new StaticLayout(TEXT1,mTextPaint,canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL,1.0F,0.0F,false);
    mStaticLayout.draw(canvas);
    canvas.restore();
}`

But in usual, For anyone still have question, the real, good answer is here:

https://stackoverflow.com/a/16883281/1154026

Community
  • 1
  • 1
Jack Lee
  • 41
  • 2