I am trying to add multiple font-families in my app. Following is code that adds "inter" font.
Here's values/typography.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="textAppearanceDisplayLargeBold" format="reference" />
<attr name="textAppearanceDisplayLargeSemiBold" format="reference" />
<attr name="textAppearanceDisplayLargeMediumBold" format="reference" />
<style name="TextAppearance.App.DisplayLarge" parent="TextAppearance.Material3.DisplayLarge">
<item name="fontFamily">@font/inter_font_family</item>
<item name="fontWeight">400</item>
</style>
<style name="TextAppearance.App.DisplayLarge.Bold">
<item name="fontWeight">700</item>
</style>
<style name="TextAppearance.App.DisplayLarge.SemiBold">
<item name="fontWeight">600</item>
</style>
<style name="TextAppearance.App.DisplayLarge.Medium">
<item name="fontWeight">500</item>
</style>
</resources>
Here's font/inter_font_family.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="@font/inter_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<font
app:font="@font/inter_medium"
app:fontStyle="normal"
app:fontWeight="500" />
<font
app:font="@font/inter_semibold"
app:fontStyle="normal"
app:fontWeight="600" />
<font
app:font="@font/inter_bold"
app:fontStyle="normal"
app:fontWeight="700" />
</font-family>
Here's themes.xml
<style name="AppTheme" parent="Theme.Material3.Light.NoActionBar">
<item name="textAppearanceDisplayLarge">@style/TextAppearance.App.DisplayLarge</item>
<item name="textAppearanceDisplayLargeBold">
@style/TextAppearance.App.DisplayLarge.Bold
</item>
<item name="textAppearanceDisplayLargeSemiBold">
@style/TextAppearance.App.DisplayLarge.SemiBold
</item>
<item name="textAppearanceDisplayLargeMediumBold">
@style/TextAppearance.App.DisplayLarge.Medium
</item>
</style>
My primary problem is that fontWeight has no effect in TextAppearance styles. If I set individual fonts (inter_bold, inter_light, etc) in TextAppearance styles, it works and shows correct fonts.
Unfortunately, the
fontWeightattribute might not be applied correctly when specifying a font family in theTextAppearancestyle.By the way, you can define separate styles for each font weight in your typography.xml file. See bellow:
In this example, each
TextAppearancestyle explicitly specifies the desired font file. This way, thefontWeightattribute should have the expected effect since it's now applied to a specific font file.Ensure to update the themes.xml file to incorporate these styles:
This should resolve the issue with the
fontWeightattribute not taking effect. EachTextAppearancestyle now explicitly specifies the font file, allowing you to control both the font family and weight.Hope it helps!