0

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

PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
Omid
  • 438
  • 4
  • 11
  • Does your two-parameter constructor also do the same setup? What's the namespace you've defined for the `custom` prefix? – Mike M. Jan 22 '18 at 07:29
  • yes it does. and `xmlns:custom="http://schemas.android.com/tools"` – Omid Jan 22 '18 at 07:31
  • Yeah, wrong namespace. It should be `xmlns:custom="http://schemas.android.com/apk/res-auto"`. – Mike M. Jan 22 '18 at 07:34
  • thanks. it was automatically added by the android studio so I didn't check the namespace, it's all working now – Omid Jan 22 '18 at 07:44
  • No problem. Btw, the `tools` namespace in a layout is for design-time features. You might still need that, if want things to look a certain way in the editor: https://developer.android.com/studio/write/tool-attributes.html. Glad you got it working. Cheers! – Mike M. Jan 22 '18 at 07:58
  • I think you need to correct tow point: 1. By convention, the custom view (TextViewPlus) and the declare-styleable (CustomFont) should have the same name (various editor features rely on this convention); 2. You'd better use xmlns:app="http://schemas.android.com/apk/res-auto", **custom:cFontWeight** change to **app:cFontWeight**. – zonda Jan 22 '18 at 07:59

0 Answers0