Search code examples
androidspannablestring

TextAppearanceSpan with style from the theme


TL;DR How can I use a style from the current theme with TextAppearanceSpan?


Let's say I am using a custom style for my app, with a custom style for Subtitle1, like this:

<style name="MyAppTheme" parent="Theme.MaterialComponents">
    <item name="textAppearanceSubtitle1">@style/MyStyle.TextAppearanceSubtitle1</item>
</style>

where my theme is no more than

<style name="MyStyle.TextAppearanceSubtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
    <item name="textSize">16sp</item>
</style>

Now I want to build a SpannedString with this custom style, so I'm using a TextAppearanceSpan

val apperSpan = TextAppearanceSpan(context, R.attr.TextAppearanceSubtitle1)
println("Your style has textSize = ${apperSpan.textSize}")

But in this case the output is Your style has textSize = -1. However, if I swap R.attr.textAppearanceHeadline4 with R.style.MyStyle_TextAppearanceSubtitle1, the textSize will be right, but this isn't theme independent.

How could I extract a TextAppearanceSpan with a style from the current theme?


Solution

  • All you need is to pass style/appearance resource id to the TextAppearanceSpan constructor, not attribute id. To resolve attribute value from the current theme use Theme#resolveAttribute method:

    val outValue = TypedValue()
    context.theme.resolveAttribute(R.attr.TextAppearanceSubtitle1, outValue, true)
    val appearance = outValue.resourceId
    //...
    val apperSpan = TextAppearanceSpan(context, appearance)
    

    or

    val typedArray = context.obtainStyledAttributes(intArrayOf(R.attr.TextAppearanceSubtitle1))
    val appearance = typedArray.getResourceId(0, 0)
    typedArray.recycle()
    //...
    val apperSpan = TextAppearanceSpan(this, appearance)