0

I have always thought that @+id was only meant for ID definitions and not references. However, while using the visual constraint editor for ConstraintLayout, I found that all references to views for constraints are automatically set using that form instead of the usual @id form.

From that, I became suspicious that maybe I was wrong. I knew the code would work if I used either form for references, so I went to the docs looking for the actual difference and turns out they say the same as I thought it was. The plus sign form is a sign for the compiler to add an ID to the R.id class. That form being used in references only works because Android manages to deal correctly with duplicates, but everyone seems to agree that the best practice is to follow the docs and not have any duplicates. I even found a comment on an SO answer saying that "It works, but only because of the Android system being forgiving of duplicate IDs (...) However, this may change in the future, which will cause such layouts to become broken..." which is kind of terrifying.

The question then is why does AS itself use the other form, conflicting with their docs? Is it a bug? Is it reported somewhere? And if it is not a bug, then what is the actual importance of using one vs. the other? Is it actually confirmed that the theory of it potentially breaking all existing layouts is false?

Iaka Noe
  • 43
  • 8
  • "I even found a comment on …" – Ignore that comment. It's silly. "The question then is why does AS itself use the other form, conflicting with their docs?" – It's not quite clear to which docs you're referring there. Do you have a link? – Mike M. Jan 06 '22 at 21:00
  • That was just an opinion from 10 years ago, there's so many layout files and tools (as you see even official editor defaults to `@+id`) in existence already that I'd say we'll sooner see xml layouts completely deprecated than them enforcing raw `@id`. – Pawel Jan 06 '22 at 21:01
  • @MikeM. Docs: https://developer.android.com/guide/topics/ui/declaring-layout#id Actually it appears to say it's the same as I read it again. Still, I was mostly afraid about that theory or the fact that nobody had ever reported it if it turned out to be a bug. – Iaka Noe Jan 06 '22 at 21:10
  • Yeah, there's really no problem with using `@+id` everywhere for your own IDs, unless you would want to enforce them being defined in only one place, for whatever reason. You might think of the `+` as meaning "create if it doesn't exist", rather than "must be created", as the docs say. When referencing system or library IDs, though, you don't want the `+`, 'cause those have already been defined. – Mike M. Jan 06 '22 at 21:17

0 Answers0