Search code examples
androidandroid-inflate

Android: Set font family to textview before inflate xml file


I want set font family to Textview

But i want this action before load xml

My code :

for(int i=0;i++;i<100000){    
    final View addNew = G.inflater.inflate(R.layout.post, null);
    TextView txtTitle = (TextView) addNew.findViewById(R.id.txtTitle);
    txtTitle.setText("" + i);
    txtTitle.setTypeface(Typeface.createFromAsset(context.getAssets(),"tahoma.ttf"));
}

This code it takes time 15-16 seconds

But if i not use setTypeface takes time 2-3 seconds


Solution

  • If you don't mind using third party libraries, I recommend you to try Calligraphy. You can find everything you need on the GitHub page, it's really easy to use.

    You just need to set it up in your Application class and Activity classes, you put your fonts in the assets folder, and then you can use the fonts from XML, like this:

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fontPath="fonts/Roboto-Bold.ttf"/>