Set font for all text elements in action?

Is it possible to set the font for all TextViews in action? I can set the font for a single textView using:

TextView tv=(TextView)findViewById(R.id.textView1); Typeface face=Typeface.createFromAsset(getAssets(), "font.ttf"); tv.setTypeface(face); 

But I would like to immediately change all the text elements, instead of manually setting them for each text element, any information would be appreciated!

+51
android fonts typeface textview
May 26 '12 at 13:36
source share
8 answers

Solution1 :: Just call this method, passing the parent view as an argument.

 private void overrideFonts(final Context context, final View v) { try { if (v instanceof ViewGroup) { ViewGroup vg = (ViewGroup) v; for (int i = 0; i < vg.getChildCount(); i++) { View child = vg.getChildAt(i); overrideFonts(context, child); } } else if (v instanceof TextView ) { ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf")); } } catch (Exception e) { } } 

Solution2 :: you can subclass the TextView class with your custom font and use it instead of 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() { if (!isInEditMode()) { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf"); setTypeface(tf); } } } 
+89
May 26 '12 at 13:47
source share

One of my personal collections:

 private void setFontForContainer(ViewGroup contentLayout) { for (int i=0; i < contentLayout.getChildCount(); i++) { View view = contentLayout.getChildAt(i); if (view instanceof TextView) ((TextView)view).setTypeface(yourFont); else if (view instanceof ViewGroup) setFontForContainer((ViewGroup) view); } } 
+8
Feb 12 '16 at 12:29
source share

If you are looking for a more general software solution, I created a static class that can be used to set Typeface for the whole view (Activity UI). Please note that I am working with Mono (C #), but you can easily implement it using Java.

You can pass this class a layout or a specific view that you want to customize. If you want to be super efficient, you can implement it using the Singleton template.

 public static class AndroidTypefaceUtility { static AndroidTypefaceUtility() { } //Refer to the code block beneath this one, to see how to create a typeface. public static void SetTypefaceOfView(View view, Typeface customTypeface) { if (customTypeface != null && view != null) { try { if (view is TextView) (view as TextView).Typeface = customTypeface; else if (view is Button) (view as Button).Typeface = customTypeface; else if (view is EditText) (view as EditText).Typeface = customTypeface; else if (view is ViewGroup) SetTypefaceOfViewGroup((view as ViewGroup), customTypeface); else Console.Error.WriteLine("AndroidTypefaceUtility: {0} is type of {1} and does not have a typeface property", view.Id, typeof(View)); } catch (Exception ex) { Console.Error.WriteLine("AndroidTypefaceUtility threw:\n{0}\n{1}", ex.GetType(), ex.StackTrace); throw ex; } } else { Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / view parameter should not be null"); } } public static void SetTypefaceOfViewGroup(ViewGroup layout, Typeface customTypeface) { if (customTypeface != null && layout != null) { for (int i = 0; i < layout.ChildCount; i++) { SetTypefaceOfView(layout.GetChildAt(i), customTypeface); } } else { Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / layout parameter should not be null"); } } } 

In your activity, you will need to create a Typeface object. I create a mine in OnCreate () using the .ttf file located in my Resources / Assets / directory. Make sure the file is marked as an Android resource in its properties.

 protected override void OnCreate(Bundle bundle) { ... LinearLayout rootLayout = (LinearLayout)FindViewById<LinearLayout>(Resource.Id.signInView_LinearLayout); Typeface allerTypeface = Typeface.CreateFromAsset(base.Assets,"Aller_Rg.ttf"); AndroidTypefaceUtility.SetTypefaceOfViewGroup(rootLayout, allerTypeface); } 
+3
Jul 30 '13 at 22:24
source share

Agarwal's answer extension ... you can set the correct, bold, italics, etc. by switching the style of your TextView.

 import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView; public class TextViewAsap extends TextView { public TextViewAsap(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public TextViewAsap(Context context, AttributeSet attrs) { super(context, attrs); init(); } public TextViewAsap(Context context) { super(context); init(); } private void init() { if (!isInEditMode()) { Typeface tf = Typeface.DEFAULT; switch (getTypeface().getStyle()) { case Typeface.BOLD: tf = Typeface.createFromAsset(getContext().getAssets(), "Fonts/Asap-Bold.ttf"); break; case Typeface.ITALIC: tf = Typeface.createFromAsset(getContext().getAssets(), "Fonts/Asap-Italic.ttf"); break; case Typeface.BOLD_ITALIC: tf = Typeface.createFromAsset(getContext().getAssets(), "Fonts/Asap-Italic.ttf"); break; default: tf = Typeface.createFromAsset(getContext().getAssets(), "Fonts/Asap-Regular.ttf"); break; } setTypeface(tf); } } } 

You can create your Assets folder as follows: Create Assets

And your Assets folder should look like this:

enter image description here

Finally, your TextView in xml should be a view of type TextViewAsap. Now it can use any style that you encoded ...

 <com.example.project.TextViewAsap android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Example Text" android:textStyle="bold"/> 
+1
Dec 21 '16 at 18:02
source share

Best answers

1. Setting up a custom font for a single textView

 Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "Fonts/FontName.ttf"); textView.setTypeface (typeface); 



2. Custom font settings for all text elements

Create a JavaClass as shown below

 public class CustomFont extends android.support.v7.widget.AppCompatTextView { public CustomFont(Context context) { super(context); init(); } public CustomFont(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomFont(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/FontName.ttf"); setTypeface(tf); } } 

And on your xml page

 <packageName.javaClassName> ... /> 

=>

  <com.mahdi.hossaini.app1.CustomFont android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="KEEP IT SIMPLE" android:textAlignment="center" /> 
+1
Sep 03 '17 at 5:20
source share

An example of a more "general" method using reflection:

** it represents the idea of ​​involving the setTextSize (int, float) child group method, but you can accept it, as in the case of your question, for setTypeFace ()

  /** * change text size of view group children for given class * @param v - view group ( for example Layout/widget) * @param clazz - class to override ( for example EditText, TextView ) * @param newSize - new font size */ public static void overrideTextSize(final View v, Class<?> clazz, float newSize) { try { if (v instanceof ViewGroup) { ViewGroup vg = (ViewGroup) v; for (int i = 0; i < vg.getChildCount(); i++) { View child = vg.getChildAt(i); overrideTextSize(child, clazz, newSize); } } else if (clazz.isAssignableFrom(v.getClass())) { /** create array for params */ Class<?>[] paramTypes = new Class[2]; /** set param array */ paramTypes[0] = int.class; // unit paramTypes[1] = float.class; // size /** get method for given name and parameters list */ Method method = v.getClass().getMethod("setTextSize",paramTypes); /** create array for arguments */ Object arglist[] = new Object[2]; /** set arguments array */ arglist[0] = TypedValue.COMPLEX_UNIT_SP; arglist[1] = newSize; /** invoke method with arguments */ method.invoke(v,arglist); } } catch (Exception e) { e.printStackTrace(); } } 

ATTENTION:

The use of reflection should be very careful. The reflection class is very " exceptional "

  • For example, you should check for annotations to prevent various problems. In the case of the SetTextSize () method, it is advisable to check the annotations android.view.RemotableViewMethod
0
Jul 05 '15 at 21:10
source share

You can use the Calligraphy library which is available here:
Android Calligraphy Library

0
Aug 12 '18 at 6:56
source share

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

Follow the steps mentioned above to use your own font or ttf in a textview, button, etc.

0
Apr 18 '19 at 10:59
source share



All Articles