I'm trying to implement a custom attribute for my custom TextView class. I followed the Creating a View Class by the letter and went through questions here like
and few more. They are all the same and they all failed to help me in this case!
so here are some of my code attr ( i added the first part
<declare-styleable name="AvatarImageBehavior">
<attr name="finalYPosition" format="dimension"/>
<attr name="startXPosition" format="dimension"/>
<attr name="startToolbarPosition" format="dimension"/>
<attr name="startHeight" format="dimension"/>
<attr name="finalHeight" format="dimension"/>
</declare-styleable>
<declare-styleable name="CustomFont">
<attr name="toPersianNumbers" format="boolean" />
<attr name="cFontWeight" format="enum">
<enum name="thin" value="0"/>
<enum name="normal" value="1"/>
<enum name="bold" value="2"/>
</attr>
</declare-styleable>
xml
<com.sahab.omida.sahab.visualImprovements.AppCompact.TextViewPlus
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/load_sub_pickup_location"
custom:cFontWeight="bold"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:textColor="?titleColor"
android:textStyle="bold" />
**function**
public static int fontWeight(AttributeSet attrs, Context ctx, int defStyle){
TypedArray a = ctx.getTheme().obtainStyledAttributes(
attrs,
R.styleable.CustomFont,
defStyle, 0);
int fontWeight;
try {
fontWeight = a.getInteger(R.styleable.CustomFont_cFontWeight,1);
Log.d("fontweight", "fontWeight: " + fontWeight);
} finally {
a.recycle();
}
return fontWeight;
}
and **one of my constructors**
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.ctx = context;
this.lang = SharedPreferenceHelper.getLang(context);
this.fontWeight = CustomFontsLoader.fontWeight(attrs,ctx,defStyle);
init();
}
the following is in the other constructor containing attrs
this.fontWeight = CustomFontsLoader.fontWeight(attrs,ctx,0);
that must be all. it must be something small that I missed. but I just can't get it to work no matter how I check it.
ps. TextView extends android.support.v7.widget.AppCompatTextView