title: How to use specified weights for fonts in XML
body: <p>Using the <a href="https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html" rel="noreferrer">Fonts in XML</a> feature you can specify various font weights for a font family. For example:</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:font=&quot;@font/archivo_narrow_regular&quot; android:fontWeight=&quot;400&quot; android:fontStyle=&quot;normal&quot;
          app:font=&quot;@font/archivo_narrow_regular&quot; app:fontWeight=&quot;400&quot; app:fontStyle=&quot;normal&quot;/&gt;
  
      &lt;font android:font=&quot;@font/archivo_narrow_regular_italic&quot; android:fontWeight=&quot;400&quot; android:fontStyle=&quot;italic&quot;
          app:font=&quot;@font/archivo_narrow_regular_italic&quot; app:fontWeight=&quot;400&quot; app:fontStyle=&quot;italic&quot;/&gt;
  
      &lt;font android:font=&quot;@font/archivo_narrow_medium&quot; android:fontWeight=&quot;500&quot; android:fontStyle=&quot;normal&quot;
          app:font=&quot;@font/archivo_narrow_medium&quot; app:fontWeight=&quot;500&quot; app:fontStyle=&quot;normal&quot;/&gt;
  
      &lt;font android:font=&quot;@font/archivo_narrow_medium_italic&quot; android:fontWeight=&quot;500&quot; android:fontStyle=&quot;italic&quot;
          app:font=&quot;@font/archivo_narrow_medium_italic&quot; app:fontWeight=&quot;500&quot; app:fontStyle=&quot;italic&quot;/&gt;
  
      &lt;font android:font=&quot;@font/archivo_narrow_semibold&quot; android:fontWeight=&quot;600&quot; android:fontStyle=&quot;normal&quot;
          app:font=&quot;@font/archivo_narrow_semibold&quot; app:fontWeight=&quot;600&quot; app:fontStyle=&quot;normal&quot;/&gt;
  
      &lt;font android:font=&quot;@font/archivo_narrow_semibold_italic&quot; android:fontWeight=&quot;600&quot; android:fontStyle=&quot;italic&quot;
          app:font=&quot;@font/archivo_narrow_semibold_italic&quot; app:fontWeight=&quot;600&quot; app:fontStyle=&quot;italic&quot;/&gt;
  
      &lt;font android:font=&quot;@font/archivo_narrow_bold&quot; android:fontWeight=&quot;700&quot; android:fontStyle=&quot;normal&quot;
          app:font=&quot;@font/archivo_narrow_bold&quot; app:fontWeight=&quot;700&quot; app:fontStyle=&quot;normal&quot;/&gt;
  
      &lt;font android:font=&quot;@font/archivo_narrow_bold_italic&quot; android:fontWeight=&quot;700&quot; android:fontStyle=&quot;italic&quot;
          app:font=&quot;@font/archivo_narrow_bold_italic&quot; app:fontWeight=&quot;700&quot; app:fontStyle=&quot;italic&quot;/&gt;
  
  &lt;/font-family&gt;
  </code></pre>
  <p>But I cannot figure out how to actually make use of each of these weights; either in an XML (layout/style) file, or in Java code. Their is no <code>fontWeight</code> attribute available for <code>TextView</code>, and the <a href="https://developer.android.com/reference/android/graphics/Typeface.html#create(android.graphics.Typeface,%20int)" rel="noreferrer"><code>Typeface</code></a> object created from <a href="https://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html" rel="noreferrer"><code>ResourcesCompat.getFont(context, R.font.archivo_narrow)</code></a> has no mention of font weights.</p>
  <p>I realize that I can just specify the specific font resource (i.e. <code>R.font.archivo_narrow_semibold</code>), but then what is the point of having a <code>fontWeight</code> attribute in the <code>font-family</code>?</p>
  <hr />
  <h3>Update</h3>
  <p>A new static <a href="https://developer.android.com/reference/android/graphics/Typeface.html#create(android.graphics.Typeface,%20int,%20boolean)" rel="noreferrer"><code>create(Typeface family, int weight, boolean italic)</code></a> method was added in API Level 28, along with a <a href="https://developer.android.com/reference/android/graphics/Typeface.html#getWeight()" rel="noreferrer"><code>getWeight()</code></a> instance method. This finally makes it possible to make use of the <code>fontWeight</code> attribute in Java code; though only for API Level 28 and above, I haven't found any analogs in the support library.</p>
  <p>This is useful—and shows that the <code>fontWeight</code> attribute didn't serve any purpose in the past—but I would really like to be able to use the weight in XML styling.</p>