Reputation: 1225
I´m trying to read attributes from a TextAppearance
style, so I can apply the values to nontrivial components (TextPaint
for example)
Ideally, I´d like to receive a style id referencing a TextAppearance and read values like text color, font, etc.
I noticed that R.styleable.TextAppearance
is internal and thus inaccessible, as well as TextAppearanceAttributes
class.
Is there an alternative to this idea?
Thanks!
Upvotes: 3
Views: 1744
Reputation: 7081
If you use Google Material Components, they have a very useful TextAppearance
class. It's restricted API, but you can just ignore that:
@SuppressLint("RestrictedApi")
val textAppearance = TextAppearance(context, R.style.TextAppearance_MaterialComponents_Body1)
All the attributes you need are directly available as members of the TextAppearance
class.
If that's not an option, then you can always just copy the code they use. Basically it involves obtaining styled attributes using the R.styleable.TextAppearance
array and its indices from R.styleable.TextAppearance_android_**
(these are private and part of the greylist):
TypedArray a = context.obtainStyledAttributes(id, R.styleable.TextAppearance);
textSize = a.getDimension(R.styleable.TextAppearance_android_textSize, 0f);
textStyle = a.getInt(R.styleable.TextAppearance_android_textStyle, Typeface.NORMAL);
// Other attributes are obtained similarly...
There's also a few tricks to inflate color resources correctly on all API levels, and getting the font isn't very straightforward. You can check out the code if you want: TextAppearance.java
.
Upvotes: 6