2

Usually al examples of custom attributes are of the form:

<declare-stylable name="MyView">
    <attr name="name" format="string"/>
</declare-styleable>  

and its usage:

<com.example.test.MyView
    customns:name="Hello"/>

So the custom view has the same name as the stylable attributes.

But in this example (click for full code) you see:

<declare-styleable name="Options">
    <attr name="titleText" format="string" localization="suggested" />
    <attr name="valueColor" format="color" />
</declare-styleable>

used by:

<com.vogella.android.view.compoundview.ColorOptionsView
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    custom:titleText="Background color"
    custom:valueColor="@android:color/holo_green_light"
    />

It made me wonder, how is it that ColorOptionsView was linked to the attributes defined with name Options?

ilomambo
  • 8,290
  • 12
  • 57
  • 106

1 Answers1

1

Those options are available as part of the declared namespace custom, which is included at the top of the XML file:

xmlns:custom="http://schemas.android.com/apk/res/com.vogella.android.view.compoundview"

NOTE

Simply adding this line will not provide support for auto-complete. If that is what your mean by your question, you need to add the schema to your workspace's XML Catalog. You can do this in Eclipse by going to Eclipse -> Preferences, then XML -> XML Catalog. Here, click the Add... button. Navigate to the XML Schema file, then select OK. If you close and re-open the the XML file, you will now have autocompletion.


Finally, when unpacking the attributes used in ColorOptionsView.java, the author can specifically look for attributes from this namespace. This is from the same source (commented by me):

//grab the declared-styleable resource entries
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Options, 0, 0);
//get the "titleText" entry from this element's attributes
String titleText = a.getString(R.styleable.Options_titleText);
//get the "valueColor" attribute. If it does not exists, set the default to holo_blue_light
int valueColor = a.getColor(R.styleable.Options_valueColor,
    android.R.color.holo_blue_light);
a.recycle();
Phil
  • 35,852
  • 23
  • 123
  • 164
  • That's the code, but how does the connection made in the XML file? Clearly when you edit the XML the auto-complete "knows" which attributes you may write. – ilomambo Apr 23 '13 at 04:57
  • @ilomambo, that is because of the `namespace` declaration, as stated above. If you want to play around this this, remove that line, then close and reopen the `XML` file. You will now notice that the autocomplete has no knowledge of those attributes. – Phil Apr 23 '13 at 05:13
  • @ilomambo, I also added some content to my response to address how to hook up XML auto-completion. – Phil Apr 23 '13 at 05:18
  • @Phill Just to be clear. You are saying that the namespace declaration directs the XML validator/editor to the java code and the editor scans the code to see which stylable is used in `obtainStyledAttributes` and that's the way it knows that `Options` is used? Do you happen to know where this mechanism is described in the official documentation, or did you deduce it just from the actual behavior? – ilomambo Apr 23 '13 at 05:19
  • @ilomambo, nothing in the XML links to Java code. the XML validator links to the Schema file. My edit about the XML catalog is a generalization however, and may not necessarily be how new `declare-styleable`s work. From what I understand, the Android XML editor validates agains the attr files themselves. This would be my deduction - but makes sense considering that there is no Android layout schema file - only declared-styleables. – Phil Apr 23 '13 at 05:41
  • @Phill That's my confusion exactly. I have my own code where I have several stylable groups. If I don't give them the exact custom view name the XML does not show the attributes in auto-complete. So I still don't see where the name `Options` is linked to the custom view in that example. The namespace surely does not contain `Options` and even if you add the file to the XML catalog, how does it know to choose `Options` for that specific view? I sound repetitive and stupid, but I don't see how it is done. I can carry on using it as I do now, but without full undestanding of what is happening. – ilomambo Apr 23 '13 at 06:46