In an XML file, we can assign an ID to a view like android:id="@+id/something" and then call findViewById(), but when creating a view programmatically, how do I assign an ID?
I think setId() is not the same as default assignment. setId() is extra.
Can anybody correct me?
Android
idoverviewAn Android
idis an integer commonly used to identify views; thisidcan be assigned via XML (when possible) and via code (programmatically.) Theidis most useful for getting references for XML-definedViews generated by anInflater(such as by usingsetContentView.)Assign
idviaXMLandroid:id="@+id/somename"to your view.android:idwill be assigned a uniqueintfor use in code.android:id‘sintvalue in code using “R.id.somename” (effectively a constant.)intcan change from build to build so never copy an id fromgen/package.name/R.java, just use “R.id.somename”.idassigned to aPreferencein XML is not used when thePreferencegenerates itsView.)Assign
idvia code (programmatically)ids usingsomeView.setId(int);intmust be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.)Uniqueness of
idsXML-assignedids will be unique.ids do not have to be uniqueids can (theoretically) conflict withXML-assignedids.ids won’t matter if queried correctly (keep reading).When (and why) conflicting
ids don’t matterfindViewById(int)will iterate depth-first recursively through the view hierarchy from the View you specify and return the firstViewit finds with a matchingid.ids assigned before an XML-definedidin the hierarchy,findViewById(R.id.somename)will always return the XML-defined View soid‘d.Dynamically Creating Views and Assigning
IDsViewGroupwithid.LinearLayoutwithandroid:id="@+id/placeholder".ViewGroupwithViews.ids that are convenient to each view.Query these child views using placeholder.findViewById(convenientInt);
API 17 introduced
View.generateViewId()which allows you to generate a unique ID.If you choose to keep references to your views around, be sure to instantiate them with
getApplicationContext()and be sure to set each reference to null inonDestroy. Apparently leaking theActivity(hanging onto it after is is destroyed) is wasteful.. 🙂Reserve an XML
android:idfor use in codeAPI 17 introduced
View.generateViewId()which generates a unique ID. (Thanks to take-chances-make-changes for pointing this out.)*If your
ViewGroupcannot be defined via XML (or you don’t want it to be) you can reserve the id via XML to ensure it remains unique:Here, values/ids.xml defines a custom
id:Then once the ViewGroup or View has been created, you can attach the custom id
Conflicting
idexampleFor clarity by way of obfuscating example, lets examine what happens when there is an
idconflict behind the scenes.layout/mylayout.xml
To simulate a conflict, lets say our latest build assigned
R.id.placeholder(@+id/placeholder) anintvalue of12..Next, MyActivity.java defines some adds views programmatically (via code):
So
placeholderand one of our newTextViews both have anidof 12! But this isn’t really a problem if we query placeholder’s child views:*Not so bad