When creating a view
, we use @+id/fullname
to identify it.
For example:
<TextView
android:id = "@+id/tv_fullname"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:text = "Fullname" />
What is the difference of using @id/tv_fullname
(without the +), instead of @+id/ tv_fullname
? Is it correct to use one or the other?
@+id
tells the system to generate a new id in theR.java
project class. While@id
it tells the system to use an id already generated in the classR.java
.@+id : indicates the definition of an id to a resource, when we use we
android:id
also indicate that an id of a resource is added insideR.java
when your application is compiled:If you define an id to a view through
android:id
and you don't use@+id
, it simply won't be recognized, that's why it's important to use@+id
, to define that it is an id that we want to assign and this is recognized in your project.@id : indicates the reference of a resource by its id, it is actually used to access the resources defined in a project, in this case those found in R.java defined by an identifier:
It's important to mention that when the resource record was generated in
R.java
it doesn't matter if you use@id
or@+id
to get a resource reference.I recommend you review the documentation:
Create a simple user interface