Monday, 15 February 2021

How to set Android Chip text appearance from currently selected Main App Theme

I have been investigating dynamically changing my Android Apps theme in my current Android project.

I have defined a base Theme then created multiple sub themes which simply employ a different font.

This all works fine and as expected for TextViews.

<style name="Theme.MyBaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:windowAnimationStyle">@style/WindowAnimations</item>
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryVariant">@color/primaryVariantColor</item>
    <item name="colorSecondary">@color/secondaryColor</item>
    <item name="colorSecondaryVariant">@color/secondaryVariantColor</item>
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <item name="android:textAppearanceLarge">@style/TextAppearance.Literal</item>
    <item name="android:fontFamily">@font/roboto_regular</item>
    <item name="fontFamily">@font/roboto_regular</item>
</style>

<style name="Theme.MyBaseTheme.Readable" parent="Theme.MyBaseTheme">
    <item name="android:fontFamily">@font/lexie_readable_regular</item>
    <item name="fontFamily">@font/lexie_readable_regular</item>
</style>

<style name="Theme.MyBaseTheme.Dyslexic" parent="Theme.MyBaseTheme">
    <item name="android:fontFamily">@font/open_dyslexic_regular</item>
    <item name="fontFamily">@font/open_dyslexic_regular</item>
</style>

<style name="Theme.MyBaseTheme.Mono" parent="Theme.MyBaseTheme">
    <item name="android:fontFamily">@font/b612_mono_regular</item>
    <item name="fontFamily">@font/b612_mono_regular</item>
</style>

I have an issue with the text displayed within com.google.android.material.chip.Chip.

namely the Chip text appearance does not change.

I can change the Chip theme using a custom theme defined as follows

<style name="ChipTextAppearance" parent="TextAppearance.MaterialComponents.Chip">
  <item name="fontFamily">@font/roboto_mono</item>
  <item name="android:fontFamily">@font/roboto_mono</item>
</style>

However I cannot see how I can how to link my custom chip theme to my App theme so that when I switch the App theme dynamically the Chip text appearance also changes to employ the correct font.

What do I need to do to dynamically switch my App theme and my Chip text appearance?



from How to set Android Chip text appearance from currently selected Main App Theme

No comments:

Post a Comment