streamride
streamride

Reputation: 567

How can I obtain styleable attributes in JAR library? attrs.xml is in main app

I have custom widget as

<com.example.MyWidget
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        custom:key="242"
        android:layout_height="wrap_content"/>

It is declared in jar library. And I have attrs.xml in main app

<declare-styleable name="MyWidget">
    <attr name="key" format="string"/>
</declare-styleable>

I have to get that value in MyWidget class.

I try to do this in constructor of MyWidget:

    TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyWidget);
            try {
                myKey = a.getString(R.styleable.MyWidget_key);
            }
            finally {
                a.recycle();
}

but there is NoClassDefFoundException of R$styleable What am I doing wrong?

Upvotes: 0

Views: 426

Answers (1)

Asier
Asier

Reputation: 46

The R.styleable.MyWidget isn't in your jar, so it always will return NoClassDefFoundException.

You have to get the attribute:

for (int i = 0; i < attrs.getAttributeCount(); i++){
    if (attrs.getAttributeName(i).equals("key")){
        myKey = attrs.getAttributeValue(i));
    }
}

Upvotes: 1

Related Questions