material-components-android
material-components-android copied to clipboard
[Chip] android:fontFamily attribute doesn't really change the font typeface
@MohamedElattar22 can you provide some more details? (explanation, code snippets, screenshots)
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:
- Use a device that allows to change the font of the OS, and change it to something very noticeable.
- 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:
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:
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:
- Download a very noticeable font into your app.
- 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?
Found a semi-workaround, semi-bug:
https://stackoverflow.com/questions/50817881/how-to-change-the-typeface-of-chip-view
- 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>
- 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:
This bug persists. The last chip component update was 2 years ago. Is it abandoned/deprecated in favor of another component?