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); }
JCKortlang Jul 30 '13 at 22:24 2013-07-30 22:24
source share