Set font for all textViews in activity? Set font for all textViews in activity? android android

Set font for all textViews in activity?


Solution1:: Just call these method by passing parent view as 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);        }    }}


The one from my personal collection:

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);    }}


If you are looking for a more general programatic solution, I created a static class that can be used to set the Typeface of an entire view (Activity UI). Note that I am working with Mono (C#) but you can implement it easily 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 could implement it using the Singleton pattern.

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 mine in the OnCreate() using a .ttf file placed in my Resources/Assets/ directory. Make sure that the file is marked as an Android Asset 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);}