I was working on Custom Control for Android, and although I tried to do what I suggested here , it looks like I'm doing wrong.
Here is my code to find out if anyone can detect the problem:
MyComponent.java
public MyComponent(Context context, AttributeSet attrs) { super(context); TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MyComponent); CharSequence myId = arr.getString(R.styleable.MyComponent_identifier); if (myId != null) { this.setIdentifier(myId.toString()); } Integer cds = arr.getInteger(R.styleable.MyComponent_cd_number, 0); if(cds != null) { this.setCds(cds); } arr.recycle(); }
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyComponent"> <attr name="cd_number" format="integer" /> <attr name="identifier" format="string" /> </declare-styleable> </resources>
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bgl="http://schemas.android.com/apk/res/my.test.package.components" android:id="@+id/table" android:layout_width="match_parent" android:layout_height="match_parent"> ... <my.test.package.MyComponent android:id="@+id/hand" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_span="2" bgl:cd_number="4" bgl:identifier="plr"/> ... </TableLayout>
When I put this, I get the following errors:
Error: resource identifier not found for attribute 'cd_number' in package 'my.test.package' error: resource identifier not found for attribute 'identifier' in package 'my.test.package'
If I change my namespace to something like:
xmlns:bgl="http://schemas.mywhatever.com/apk/res/my.test.package"
... errors go and the thing works, but myId is null and cds is 0 (the default value!) in the constructor of MyComponent.java.
I would say that this is some very simple mistake, but I can’t notice it, and since there is not much documentation there, I decided to ask here.
Thanks in advance!