user204884
user204884

Reputation: 1795

Programmatically assign resource ids

I am creating views programmatically and later want to call 'addRule' and pass their id to this function.

Is it safe to assign ids programmatically?
How do I know that I am not overriding existing ids?
Is there a resource type '0' or can I presume that any resource id I create of the format 0xNNTTNNNN where 'TT' is the type = 0 is ok?

Upvotes: 1

Views: 829

Answers (2)

Alexander
Alexander

Reputation: 48272

You can define some ids in an xml file, say, ids.xml in the res/values directory and then you can assign these ids either programmaically or by referring to them in other xml files (say, in your layout files). This way you can, for example, guarantee that a certain view will have the same known id in all the layouts where you want it to appear

You define an id as follows:

<resources>
  <item type="id" name="myid" />
</resources>

And refer to it as usual "@id/myid"

Upvotes: 1

Noureddine AMRI
Noureddine AMRI

Reputation: 2942

EDIT:

Use View.setId (int id)

Quote:

Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.

Upvotes: 1

Related Questions