I have 2 fonts. Lacosta and Dagos

There is also a ready application. I need in this application in all Activity (only 10), replace with the font Dagos.

How to do it quickly? not to change the font for each View. for it is utter crap

In short, I decided to create a class

public class MyTextView extends TextView { public MyTextView(Context context) { super(context); Typeface typeface= Typeface.createFromAsset(context.getAssets(), "pfagorasanspro-medium.ttf"); this.setTypeface(typeface); } 

}

And replaced everything with MyTextView.

Now I get an error. What to do?

 Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to com.eranewgames.donatello.MyView.MyTextView at com.eranewgames.donatello.Auth.onCreate(Auth.java:33) 
  • And in what forgive crap? Unless search and measurements on the whole project (Ctrl + Shift + R) will not allow to cope with it in a couple of minutes? - xkor
  • I'll try to accomplish my goal - Andro

2 answers 2

You can inherit TextView and apply a font to it programmatically, and throughout your application you can use your custom TextView

 import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; public class TextViewPlus extends TextView { private static final String TAG = "TextView"; public TextViewPlus(Context context) { super(context); } public TextViewPlus(Context context, AttributeSet attrs) { super(context, attrs); setCustomFont(context, attrs); } public TextViewPlus(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setCustomFont(context, attrs); } private void setCustomFont(Context ctx, AttributeSet attrs) { TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus); String customFont = a.getString(R.styleable.TextViewPlus_customFont); setCustomFont(ctx, customFont); a.recycle(); } public boolean setCustomFont(Context ctx, String asset) { Typeface tf = null; try { tf = Typeface.createFromAsset(ctx.getAssets(), asset); } catch (Exception e) { Log.e(TAG, "Could not get typeface: "+e.getMessage()); return false; } setTypeface(tf); return true; } } 

attrs.xml: (in res / values)

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TextViewPlus"> <attr name="customFont" format="string"/> </declare-styleable> </resources> 

Put "Dagos.ttf" in the assets folder

 <com.example.TextViewPlus android:id="@+id/textViewPlus1" android:layout_height="match_parent" android:layout_width="match_parent" android:text="@string/showingOffTheNewTypeface" foo:customFont="saxmono.ttf"> </com.example.TextViewPlus> 
  • You can if you write a program from scratch. And I have 500 TextViews. And it’s dangerous to do a global search because it can wipe a lot of things - Andro

You can write a recursive function that will take the root layout as input and set the font, respectively, with checks