ilomambo
ilomambo

Reputation: 8350

Android's resource "@id" mechanism to assign id in custom XML file

Is it possible to use the @id/name and @+id/name mechanism used in Android XML files on my own custom defined XML files?

Inside an Android project I have an XSD schema file and several custom XMLs using that schema.
Some elements have an id field, which I would like to assign the same way as I assign ids to android elements; to ensure that across my custom XML files all ids are unique.

Edit
The original question was poorly described. Let me clarify:
The @id mechanism is usable inside custom XML files, I can write

<myelem id="@+id/name"/>

the problem is when I try to validate the XML file @id/name (or @+id/name) is not recognized as unsignedInt, which is the type of the id field.

cvc-attribute.3: The value '@+id/name' of attribute 'id' on element 'myelem' is not valid with respect to its type, 'unsignedInt'.

Upvotes: 2

Views: 716

Answers (1)

alex
alex

Reputation: 6409

You can define IDs in a resources XML file and use that in your XML.

<item name="idname" type="id" />

And then, when you parse your XML you can compare the value with the one from the resources.

int id = getResources().getIdentifier(idname, "id", context.getPackageName());

Upvotes: 2

Related Questions