Search code examples
androidandroid-viewandroid-identifiers

should every View have an id?


I was wondering of two possible ways of handling View IDs in Android:

  1. automatically assign an ID to every View you create
  2. assign IDs only when you need them

I can think of some pros and cons of both approaches (more for the first option) but I want to know if I am right.

Edit: I know you don't have to assign IDs to Views, I wanted to know whether I should (for future's development sake)


Solution

  • This is really up to you because it doesn't necessarily have any particular effect on efficiency, depending how you automatically add the IDs. My approach is to only assign IDs as you need them. The reason for this is because you only need the ID to reference the view or component in its controller or if you are using RelativeLayout and placing your other views/components based on other components. Otherwise it's extraneous code that doesn't really get used.