Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: how to set custom font for entire application

I have developed a very huge application and now i have a requirement of having custom font for all controls in the application. so I want to know the better way to change the font in one shot. The application has more than a hundred XML layout. and i cant change all controls to a custom component with custom font. Please provide a solution to Change the font without altering all the controls in XML.

like image 767
Santosh Avatar asked Dec 20 '22 22:12

Santosh


1 Answers

Do something like this

pacage com.prac;
class MyFontedTextView extends TextView {
    public FontedTextView(Context context) {
        super(context);
        init();
    }

    public FontedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FontedTextView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
         init();
    }

    private void init() {
     String otfName = "MyCustomOtfFileWhichIPutInAssetsFolder.otf";
     Typeface font = Typeface.createFromAsset(context.getAssets(), otfName);
     this.setTypeface(font);
    }
}

Now replace this all over in xml file from your TextViews

<com.prac.MyFontedTextView ....        instead of <TextView

This change you have to do all over for it to apply

also for the case of button text . Button is also subclass of TextView So the same can work for button's too

Hope this help or can lead you to the solution you are looking

like image 136
Rohit Sharma Avatar answered Dec 27 '22 13:12

Rohit Sharma