I have a custom TextView, implemented essentially the same as in this blog post which defaults to a certain font, but uses the textStyle
attribute to set a different font for normal, bold, or italic styles.
The constructor has a check for the textStyle
which sets the font.
MyFontTextView.java
public MyFontTextView(Context context, AttributeSet attrs) {
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
switch (textStyle) {
case Typeface.BOLD: // bold
Typeface boldFont = Typeface.createFromAsset(Application.getContext().getAssets(), "fonts/boldFont.otf");
super.setTypeface(boldFont);
...
}
}
The problem is that if I set the textStyle
in an inherited style
, it DOES NOT detect it and
getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL)
always returns my default Typeface.NORMAL
:
<style name="TextView_MyInfo">
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">true</item>
</style>
myfragment.xml
<com.myapp.views.MyFontTextView
android:id="@+id/myinfo_name"
style="@style/TextView_MyInfo"
tools:text="John Smith" />
DOESN'T SET BOLD.
But if I instead set the textStyle
directly on the element like this:
<com.myapp.views.MyFontTextView
android:id="@+id/myinfo_name"
android:textStyle="bold"
tools:text="John Smith" />
it DOES detect it and getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL)
returns Typeface.BOLD just as it should. I also know that it IS loading the styles.xml
properties correctly because it always gets the textAllCaps
attribute, along with a few others.
Do I need to be accessing the attributes set in styles.xml
differently than directly set attributes?
Based on this answer, if I can at least get the style tag set with style="@style/TextView_MyInfo"
, I could use that to check for textStyle
defined there as well.
Other Info:
- compileSdkVersion 23
- minSdkVersion 19
- targetSdkVersion 22