-1

i am working on implementation of custom font in android application..i want to use one custom font for entire application using styles.XML or may be another options is any.

2 Answers2

2

There is no easy built-in way to do this in Android.

You might want to check out Calligraphy, an open source project that makes it easy to change the font for a whole app.

Litrik De Roy
  • 823
  • 5
  • 9
0

I had the same problem and I didn't find a way to do it with .xml files, at end I inheritance TextView and EditText and apply the font by code

  1. put the .otf files at assets/fonts library in your project

  2. create a TextViewFont class that inheritance from TextView

    public class TextViewFont extends TextView {
    private int mType = 0;

    public TextViewFont(Context context) 
    {
        this(context,null,0);
    }
    
    public TextViewFont(Context context, AttributeSet attrs)
    {
    
        this(context,attrs,0);
    }
    
    
    public TextViewFont(Context context, AttributeSet attrs, int defStyle)
    {
        super(context,attrs,defStyle);
        init(context,attrs);
    }
    public void setType(int type){
        this.mType= type;
        Typeface tf;
        switch (mType)
        {
        case 0:
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xxx-light.otf");
            setTypeface(tf);
            break;
        case 1:
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xxx-regular.otf");
            setTypeface(tf);
            break;
        case 2:
            tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/xxx-bold.otf");
            setTypeface(tf);
            break;
        }
    }
    private void init(Context context, AttributeSet attrs )
    {
        if (attrs != null)
        {
            TypedArray a = context.getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.TextViewFont,
                    0, 0);
    
            try 
            {
                TypedValue tv = new TypedValue();
                if (a.getValue(0, tv))
                {
                    mType = (int)tv.data;
                }
                mType = a.getInteger(R.styleable.TextViewFont_fontType,0);
            } 
            finally 
            {
                a.recycle();
            }
        }
        if (!isInEditMode())
        {
            Typeface tf;
            switch (mType)
            {
            case 0:
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/xxx-light.otf");
                setTypeface(tf);
                break;
            case 1:
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/xxx-regular.otf");
                setTypeface(tf);
                break;
            case 2:
                tf = Typeface.createFromAsset(context.getAssets(), "fonts/xxx-bold.otf");
                setTypeface(tf);
                break;
            }
        }
    }
    

    }

3.Using example inside xml layout file

in the header of the file you use the custom view add

     xmlns:custom="http://schemas.android.com/apk/res-auto"

and add the custom class as any other view element

<xxxx.xxxx.xxxx.TextViewFont
        android:id="@+id/xxxxx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="abcdEFGH 123"
        android:textAppearance="@style/xxxxxxx"
        custom:fontType="Regular" />
  1. define the custom xml attributes vlaues/attrs.xml

<resources>
    <declare-styleable name="TextViewFont">
        <attr name="fontType" format="enum">
            <enum name="Light" value="0"/>
            <enum name="Regular" value="1"/>
            <enum name="Bold" value="2"/>
        </attr>
    </declare-styleable>
</resources>