Search code examples
androidstylesassetscalligraphy

how to get font folder in xml or java


hi i want set font but i have a problem i use calligraphy library for changing font

 new CalligraphyConfig.Builder()
                                .setDefaultFontPath("fonts/IRANSansMobile.ttf")
                                .build()))

but this get font from Assets folder! now i want change bottom navigation font but now i cant access Assets folder!

<style name="Widget.BottomNavigationView" parent="Widget.Design.BottomNavigationView">
    <item name="fontFamily">@myfont/iransansmobile</item>
    </style>

so this is my question:
1.how can i set font in setDefaultFontPath from res/myfont folder?
2.how can i set font in from Assets folder?


Solution

  • Hello i found this solution from How to use custom font in android xml?

    package com.vins.test;
    
    import android.content.Context;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class MyTextView extends TextView {
    
        public MyTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        public MyTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public MyTextView(Context context) {
            super(context);
            init();
        }
    
        private void init() {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                                   "your_font.ttf");
            setTypeface(tf);
        }
    
    }
    

    XML:

    <com.vins.test.MyTextView
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:text="This is a text view with the font u had set in MyTextView class "
        android:textSize="30dip"
        android:textColor="#ff0000"
       >
    

    Good luck.