material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[Chip] android:fontFamily attribute doesn't really change the font typeface

Open MohamedElattar22 opened this issue 1 year ago • 4 comments

MohamedElattar22 avatar Feb 08 '24 22:02 MohamedElattar22

@MohamedElattar22 can you provide some more details? (explanation, code snippets, screenshots)

dsn5ft avatar Feb 12 '24 16:02 dsn5ft

I have this issue too. Not only that, but setting the fontFamily for the app/activity doesn't affect the chip either.

@dsn5ft Please try this to notice the bug:

  1. Use a device that allows to change the font of the OS, and change it to something very noticeable.
  2. In your app's code, change the fontFamily to a specific font, preferably of a font file you've downloaded. You can set it globally by setting it to the theme of the Activity/app, and you can do it specifically to the Chip itself

The bug is that you will see that it doesn't do anything, and instead will still use what the OS was set to use.

Here's a sample project:

My Application.zip

Snippet:

        <com.google.android.material.chip.Chip
            android:text="this should be roboto font using text"
            android:fontFamily="sans-serif"
            android:layout_width="wrap_content" android:layout_height="wrap_content"/>

        <com.google.android.material.chip.Chip
            android:text="this should be roboto font using a font file"
            android:fontFamily="@font/roboto"
            android:layout_width="wrap_content" android:layout_height="wrap_content"/>

        <com.google.android.material.chip.Chip
            android:text="this should be a special font using a font file"
            android:fontFamily="@font/aguafina_script"
            android:layout_width="wrap_content" android:layout_height="wrap_content"/>

And a screenshot to prove it, right from the IDE itself:

image

This was tested on Xiaomi device with Android 10 and Samsung device with Android 13. For devices that don't have this functionality, sadly, you will have to do this instead:

  1. Download a very noticeable font into your app.
  2. Set fontFamily to use it. You can set it globally by setting it to the theme of the Activity/app, and you can do it specifically to the Chip itself

The bug is that it won't change to this font.

Something else that I've noticed, is that when I use android:fontFamily="sans-serif" on anything, it also often get ignored on many Views, and not just here. That's even though I've tested and Roboto font does exist on the devices I've tested. Can you please explain why?

AndroidDeveloperLB avatar Apr 17 '24 12:04 AndroidDeveloperLB

Found a semi-workaround, semi-bug:

https://stackoverflow.com/questions/50817881/how-to-change-the-typeface-of-chip-view

  1. Create a new style for each font you want to use for chips. Example:
<style name="ChipTextAppearance__Roboto" parent="TextAppearance.MaterialComponents.Chip">
    <item name="android:fontFamily">@font/roboto</item>
</style>

<style name="ChipTextAppearance__Special" parent="TextAppearance.MaterialComponents.Chip">
    <item name="android:fontFamily">@font/aguafina_script</item>
</style>
  1. Use it in every chip you use:
<com.google.android.material.chip.Chip
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="this should be roboto font using a font file" android:textAppearance="@style/ChipTextAppearance__Roboto" />


<com.google.android.material.chip.Chip
    style="@style/ChipTextAppearance__Special"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="this should be a special font using a font file" />

For some reason this works fine for the first font, but not the other. It occurs on both the IDE and the device:

image

My Application.zip

AndroidDeveloperLB avatar Apr 17 '24 12:04 AndroidDeveloperLB

This bug persists. The last chip component update was 2 years ago. Is it abandoned/deprecated in favor of another component?

PhantomLord72 avatar Aug 22 '24 07:08 PhantomLord72