2

I have made an Android library that contains visual components. This components style must be customizable by the main hosting application via resources files.

In order to let the font family customizable I have defined a string

<string name="customizableFontFamily" translatable="false">sans-serif</string>

Which is used in the main theme style:

<style name="MyMainTheme" parent="Theme.AppCompat.Light.NoActionBar">
   <item name="android:fontFamily">@string/customizableFontFamily</item>

This works fine when using the basic Android fonts (casual, monospace, sans-serif, etc). However, if the user wants to define a custom font it does not work since it has to point to a reference:

<string name="customizableFontFamily" translatable="false">@font/myFont</string>

Is there any way to accomplish this?

profesor_falken
  • 559
  • 6
  • 10

1 Answers1

1

You can define a custom TextAppearance and change your ui component to work with this instead of a simple fontFamily.

Something like:

<style name="TextAppearance.MyApp.Headline1" parent="TextAppearance.MaterialComponents.Headline1">
  ...
  <item name="fontFamily">@font/custom_font</item>
  <item name="android:textStyle">normal</item>
  <item name="android:textAllCaps">false</item>
  <item name="android:textSize">64sp</item>
  <item name="android:letterSpacing">0</item>
  ...
</style>

In this way the apps which use your lib can customize these values.
Soon all components included in the Material Design Library will reference these themeable text attributes.
You can read more here.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    I think this answer as the good one. In my case I just want to let change the font family on all texts so I have created an style which inherits TextAppearance.AppCompat, defines only the fontfamily and is extended for all the elements of my library. This is easily overridable by the app that adds my library so I is a perfect solution. – profesor_falken Sep 04 '19 at 12:06