Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library overrides theme's android:fontFamily #2812

Open
7 tasks done
nikclayton opened this issue Oct 10, 2024 · 0 comments
Open
7 tasks done

Library overrides theme's android:fontFamily #2812

nikclayton opened this issue Oct 10, 2024 · 0 comments

Comments

@nikclayton
Copy link

About this issue

The library overrides/ignores the theme's android:fontFamily, making it more difficult than it should be to use custom font families in an app using the library.

My app (https://pachli.app) allows the user to set a custom font. This is achieved by providing a number of different styles that override android:fontFamily, and applying the correct style at runtime based on the value of a user's preference.

For example, in styles.xml:

<style name="FontAtkinsonHyperlegible">
    <item name="android:fontFamily">@font/atkinson_hyperlegible</item>
</style>

<style name="FontEstedad">
    <item name="android:fontFamily">@font/estedad</item>
</style>

<style name="FontLexend">
     <item name="android:fontFamily">@font/lexend</item>
</style>

and in a base Activity.onCreate():

val fontFamily = EmbeddedFontFamily.from(sharedPreferencesRepository.getString(PrefKeys.FONT_FAMILY, "default"))
if (fontFamily !== EmbeddedFontFamily.DEFAULT) {
    getTheme().applyStyle(fontFamily.style, true)
}

This works everywhere except menu items.

For that I have to do this workaround after the menu is modified.

/**
 * Sets the correct typeface for everything in the drawer.
 *
 * The drawer library forces the `android:fontFamily` attribute, overriding the value in the
 * theme. Force-ably set the typeface for everything in the drawer if using a non-default font.
 */
private fun updateMainDrawerTypeface(fontFamily: EmbeddedFontFamily) {
    if (fontFamily == EmbeddedFontFamily.DEFAULT) return

    val typeface = ResourcesCompat.getFont(this, fontFamily.font) ?: return
    for (i in 0..binding.mainDrawer.adapter.itemCount) {
        val item = binding.mainDrawer.adapter.getItem(i)
        if (item !is Typefaceable) continue
        item.typeface = typeface
    }
}

(EmbeddedFontFamily is an enum with properties for the font resource and style resource).

Since the code only seems to use sans-serif and sans-serif-medium I think you might get the effect you want by (untested):

  1. Removing android:fontFamily="sans-serif" everywhere.
  2. Replacing android:fontFamily="sans-serif-medium" with android:textFontWeight="500", as "500" is the "medium" font weight.

Details

  •  Used library version: 9.0.2

Checklist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant