title: Custom Fonts and Custom Textview on Android
body: <p>From an application I need to develop, I've received a specific font that has many files like FontName-Regular, <strong>FontName-Bold</strong>, <em>FontName-it</em>. I need to use it in all the textviews in the application. First I thought it was an easy task. Look over SO and found a very nice thread:<a href="https://stackoverflow.com/questions/6926263/add-custom-font-for-complete-android-application">here</a></p>
  
  <p>So first I did like:</p>
  
  <pre><code>public static void overrideFonts(final Context context, final View v) {
      try {
          if (v instanceof ViewGroup) {
              ViewGroup vg = (ViewGroup) v;
              for (int i = 0; i &lt; vg.getChildCount(); i++) {
                  View child = vg.getChildAt(i);
                  overrideFonts(context, child);
              }
          } else if (v instanceof TextView) {
              ((TextView)v).setTypeface(FONT_REGULAR);
          }
      } catch (Exception e) {
          e.printStackTrace();
          // ignore
      }
  }
  </code></pre>
  
  <p>And called this method during onCreate in my activity. Every textView in my app was showing that font and boy, was I happy for getting away so easy. Until I got to a screen where some textviews required <strong>Bold</strong> as <code>Style</code> (<code>android:textStyle="bold"</code>). Then I realized that this solution does not provide me with possibility to load the <strong>Font-Bold</strong>.ttf from assets. </p>
  
  <p>Than looked further and saw a nice custom TextView implementation, in the same SO question:</p>
  
  <pre><code>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();
      }
  
      public void init() {
  
          Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
          setTypeface(tf ,1);
  
      }
      }
  </code></pre>
  
  <p>This looks even better. My question is: how can I detect on  <code>init()</code> if my control has Style set to Bold or not so I can assign the requested TypeFace ?</p>
  
  <p>Thank you for your time.</p>
  
  <p>LE. Following the example below, I've updated my class as:</p>
  
  <pre><code>public class MyTextView extends TextView {
  
      Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);
      Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(),  Constants.FONT_BOLD);
  
      public MyTextView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
  
      public MyTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public MyTextView(Context context) {
          super(context);
      }
  
      public void setTypeface(Typeface tf, int style) {
          if (style == Typeface.BOLD) {
              super.setTypeface(boldTypeface/*, -1*/);
          } else {
              super.setTypeface(normalTypeface/*, -1*/);
          }
      }
  }
  </code></pre>
  
  <p>Well If I debug, the app goes in setTypeFace and it seems to apply the bold one, but on my layout I can't see any change, not bold. No matter what font I use, no changes are done in my TextView and is displayed with the default android font.  I wonder why ?</p>
  
  <p>I have summed everything on a blog post <a href="http://alinberce.wordpress.com/2012/01/25/using-custom-fonts-in-android-bold-italic-regular-you-name-it/" rel="nofollow noreferrer">here on my blog</a> maybe it will help someone.</p>