Android user control namespace issue

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!

+6
source share
2 answers

Ok I decided!

In 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 I needed to put the URI in the component package.

IT IS NOT RIGHT!

In xmlns, this must be the name of the application that is declared in the manifest!

When I removed the xmlns “component” part, it “coincided” with the application name in the manifest and the errors went away, and when I ran this thing in debugging, I could actually see the values ​​that I passed to the parameters in XML!

Hope this helps someone else !:-)

UPDATE

Later I had a need to move the control to the library and the problem again. It looks like when you put a component in a library and use it in a client application, you should declare xmlns as follows:

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

If you do this (and the library is declared as a dependency on Android), Eclipse (or is it Android?) Will look for dependencies for the corresponding attribute bindings.

+14
source

I had a problem like this, it turned out that it was calling another constructor

Try a constructor that takes a defStyle parameter

 public MyComponent(Context context, AttributeSet attrs, int defStyle) 
0
source

Source: https://habr.com/ru/post/910958/


All Articles