I want to extract several attributes from the style resource (only interested in the attributes included in the TextAppearance group)
Style defined as
<style name="Label" parent="@android:style/TextAppearance.Small"> <item name="android:textColor">@color/floatlabel_text</item> <item name="android:textSize">8dp</item> <item name="android:textStyle">bold</item> </style>
Try first
At first I tried to use TextView (lines 663-731), but then it turned out that we did not have access to com.android.internal.R
Partial solution
This is why I switched to this solution: stack overflow
So, I created textAppearanceAttr to replace com.android.internal.R.styleable.TextAppearance (contains only 10/13 TextAppearance attributes that interest me)
int[] textAppearanceAttr = new int[]{ android.R.attr.textColor, android.R.attr.textSize, android.R.attr.typeface, android.R.attr.fontFamily, android.R.attr.textStyle, android.R.attr.textAllCaps, android.R.attr.shadowColor, android.R.attr.shadowDx, android.R.attr.shadowDy, android.R.attr.shadowRadius};
This is how I used it. I get the style resource identifier (the resource refers to the clTextAppearance attribute)
int ap = a.getResourceId(R.styleable.CustomLabelLayout_clTextAppearance, android.R.style.TextAppearance_Small); TypedArray appearance = mContext.obtainStyledAttributes(ap, textAppearanceAttr);
And this is how I get the attributes (still following the answers from the link above):
mLabelTextColor = appearance.getColorStateList(0); mLabelTextSize = appearance.getDimensionPixelSize(1, 15); mLabelTypeface = appearance.getInt(2, -1); mLabelFontFamily = appearance.getString(3); mLabelTextStyle = appearance.getInt(4, -1); (5 more...)
Current issue
It seems that only the first attribute is set, each other either sets the default value, or null.
A hack that seems to work
Individual arrays:
int[] textSizeAttr = new int[] { android.R.attr.textSize}; int[] textStyleAttr = new int[] { android.R.attr.textStyle};
And get these attributes
appearance.recycle(); appearance = mContext.obtainStyledAttributes(ap, textSizeAttr); mLabelTextSize = appearance.getDimensionPixelSize(0, 15); appearance.recycle(); appearance = mContext.obtainStyledAttributes(ap, textStyleAttr); mLabelTextStyle = appearance.getInt(0, -1); appearance.recycle();
Now it's such a waste.
Questions
- I would like to know why getting all attributes at once doesn't work.
- Is there a solution (where all the extra work is not needed)?
EDIT 1
I found something similar here: https://stackoverflow.com/a/3/9129/ and for some reason this works. Until I add more attributes to the array, everything becomes ker.
Example:
int[] attrs = {android.R.attr.textColor, android.R.attr.textSize, android.R.attr.background, android.R.attr.textStyle, android.R.attr.textAppearance, android.R.attr.textColorLink, android.R.attr.orientation, android.R.attr.text};
If I get text using the specified array, it works.
String text = ta.getString(7);
But if I changed the array to below, it did not work (replaced android.R.attr.orientation with android.R.attr.shadowColor)
int[] attrs = {android.R.attr.textColor, android.R.attr.textSize, android.R.attr.background, android.R.attr.textStyle, android.R.attr.textAppearance, android.R.attr.textColorLink, android.R.attr.shadowColor, android.R.attr.text};
Why is this happening? (Question No. 1)