Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Custom Control namespace issue

I've been working on a Custom Control for Android and although I tried to do what's suggested here there seems to be something I'm doing wrong.

Here's my code to see if anyone can spot 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: No resource identifier found for attribute 'cd_number' in package 'my.test.package' error: No resource identifier 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"

...the errors go way and the thing runs but myId is null and cds is 0 (the default value!) back on the MyComponent.java constructor.

I'd say it's some very basic mistake but I not being able to spot it and since there's not much documentation on this I decided to ask here.

Thanks in advance!

like image 803
Diesel Heart Avatar asked Mar 16 '12 23:03

Diesel Heart


1 Answers

Ok. I got it solved!

On the original post I had:

xmlns:bgl="http://schemas.android.com/apk/res/my.test.package

...but in my source I had:

xmlns:bgl="http://schemas.android.com/apk/res/my.test.package.components

...because I thought one should put the URI to the components package.

THIS IS WRONG!

On the xmlns it should be the application name as is declared on the Manifest!

When I removed the "components" part of the xmlns it "matched" the application name in the Manifest and the errors went away and when I ran the thing in debug I could actually see the values I was passing to the parameters in the XML!

Hope this helps someone else! :-)

UPDATE

Later on I had the need to move the control into a library and faced the problem again. It seems that when you put the component in a library and use it on a client app you must declare the xmlns as below:

 xmlns:myns="http://schemas.android.com/apk/res-auto"

If you do so (and have the library declared as an Android dependency) Eclipse (or is it Android?) will search the dependencies for the appropriate attribute bindings.

like image 84
Diesel Heart Avatar answered Oct 31 '22 16:10

Diesel Heart