title: Android custom fonts not working programmatically
body: <p>I need to change my app's font to a custom font. I'm using Android Native Fonts for this. I have added my_font.ttf and my_font_italic.ttf on the font folder.</p>
  <p>Also, I created this font family resource</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;font-family xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
           xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
      &lt;font
          android:fontStyle=&quot;normal&quot;
          android:font=&quot;@font/my_font&quot;
          app:fontStyle=&quot;normal&quot;
          app:font=&quot;@font/my_font&quot;/&gt;
  
      &lt;font
          android:fontStyle=&quot;italic&quot;
          android:font=&quot;@font/my_font_italic&quot;
          app:fontStyle=&quot;italic&quot;
          app:font=&quot;@font/my_font_italic&quot;/&gt;
  &lt;/font-family&gt;
  </code></pre>
  <p>On my layout, I have a LinearLayout with a couple of TextViews like this:</p>
  <pre><code>&lt;LinearLayout
      android:id=&quot;@+id/test&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:orientation=&quot;vertical&quot;&gt;
  
      &lt;TextView
          android:textAppearance=&quot;@style/text_28_italic&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:fontFamily=&quot;@font/my_font&quot;
          android:text=&quot;Test Text&quot;/&gt;
  
      &lt;TextView
          android:textAppearance=&quot;@style/text_28_bold&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:fontFamily=&quot;@font/my_font&quot;
          android:text=&quot;Test Text&quot;/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>The styles I'm using are:</p>
  <pre><code>&lt;style name=&quot;text_28_italic&quot;&gt;
      &lt;item name=&quot;android:fontFamily&quot;&gt;@font/my_font&lt;/item&gt;
      &lt;item name=&quot;android:textSize&quot;&gt;28sp&lt;/item&gt;
      &lt;item name=&quot;android:textStyle&quot;&gt;italic&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name=&quot;text_28_bold&quot;&gt;
      &lt;item name=&quot;android:fontFamily&quot;&gt;@font/my_font&lt;/item&gt;
      &lt;item name=&quot;android:textSize&quot;&gt;28sp&lt;/item&gt;
      &lt;item name=&quot;android:textStyle&quot;&gt;bold&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  <p>If I test the app, the fonts in the text views are shown correctly.</p>
  <p>However, the <strong>problem</strong> comes when I try to add TextViews by code. According to the <a href="https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html" rel="nofollow noreferrer">documentation</a>, I need to use <code>Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);</code> to set the font by code. But I also need to set a style so I'm using <code>setTextAppearance()</code> too.</p>
  <p>Here is my code:</p>
  <pre><code>TextView tv1 = new TextView(this);
  tv1.setText(&quot;Test text&quot;);
  tv1.setTypeface(ResourcesCompat.getFont(this, R.font.my_font));
  tv1.setTextAppearance(this, R.style.text_28_italic);
  ((LinearLayout) findViewById(R.id.test)).addView(tv1);
  
  TextView tv2 = new TextView(this);
  tv2.setText(&quot;Test text&quot;)
  tv2.setTextAppearance(this, R.style.text_28_bold);
  tv2.setTypeface(ResourcesCompat.getFont(this, R.font.my_font));
  ((LinearLayout) findViewById(R.id.test)).addView(tv2);
  </code></pre>
  <p>This way, I cannot make it work. I get the font applied, but not the text appearance.</p>
  <p>How can I make both text appearance and typeface work programmatically?</p>
  <p>Thanks!</p>