3

I am new to Android development.

I noticed that sometimes @+id was used instead of @id. From what I could find, apparently when you compile a program, it first scans the source data and the @+id tells it to add the id to the R file, and then after that it is considered okay to use @id.

Is there a good practice to use for this? Could I theoretically always use @+id to be safe? What about using @+id once to add it to R, and then removing the plus sign now that it's already present?

Or is there an accepted practice of where the @+id version is to be declared if @id is to be used everywhere else?

amallard
  • 1,189
  • 1
  • 12
  • 24
DoubleBass
  • 1,143
  • 4
  • 12
  • 29
  • 4
    Answered [here](http://stackoverflow.com/questions/11160954/what-is-the-difference-between-id-and-id) – Joe Maher Mar 05 '16 at 04:06
  • @JoeMaher I understand that (which is why I mentioned this in the OP) -- I am asking about practices – DoubleBass Mar 05 '16 at 04:09
  • Do you want any example ? – Amit Vaghela Mar 05 '16 at 04:11
  • I just generally follow the terms that if you are assigning an id to a view use @+id, when referring to a view use @id – Joe Maher Mar 05 '16 at 04:11
  • Check [here](http://android-wtf.com/2012/11/difference-between-at-plus-id-and-at-id-in-android/) and [here](http://stackoverflow.com/a/5731533/1435985) for answers. Both give reasons why you might not want to always uses @+id on everything. – George Mulligan Mar 05 '16 at 04:12
  • @GeorgeMulligan So if I am always using `@+id` then there is a risk that when I typo something, it'll think I'm creating a new label instead of trying to reference a pre-existing one, and compile without error (whereas I'd really want it to error out and tell me I typoed something) – DoubleBass Mar 05 '16 at 04:17
  • Correct. You can try it yourself real fast by trying to reference a resource you haven't given an id to. You will get an error stating a resource was not found with the given name. If you add the `+` the application will compile and only give an error in the file. – George Mulligan Mar 05 '16 at 04:20
  • @GeorgeMulligan Is it considered bad practice to mess with R.java? – DoubleBass Mar 05 '16 at 04:24
  • I would certainly say so. It is an auto generated file and I have never come across a reason to edit it myself. Can't say the same for others. – George Mulligan Mar 05 '16 at 04:27

2 Answers2

4

I think generally if you are assigning an id to a view @+id, referring to a view @id

<Button 
   android:id="@+id/start"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
/>

<Button 
   android:id="@+id/check"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@id/start"
/>
Joe Maher
  • 5,354
  • 5
  • 28
  • 44
  • So basically use `@+id` when assigning something to android:id (since presumably this is the first time it's being used). Is there any downside to me just adding something to R.java directly and then never using `@+id`? – DoubleBass Mar 05 '16 at 04:16
  • I tend to try and stay away from touching auto generated files, but each to their own – Joe Maher Mar 05 '16 at 04:17
2

@+id is used at the first occurrence of that id . This of course implies that whenever we are assigning an id the @+id is used. Besides this practice, whenever we are referring to an id for first time, eg in RelativeLayout if we use above for a particular id, and we assign that id to an element (Button or TextView) below that line of code, we have to use ...above="@+id/not_yet_assigned" For example:

       <Button
        android:id="@+id/btn_first"
        android:layout_above="@+id/btn_second"
        android:text="ONE"
        />
       <Button
        android:id="@id/btn_second"
        android:text="TWO"
        />
inkedTechie
  • 684
  • 5
  • 13