Imagine you have a ConstraintLayout
. It contains 2 views with one at the right of the other:
<androidx.constraintlayout.widget.ConstraintLayout ...>
<View android:id="@+id/left_view" ... />
<View android:id="@+id/right_view"
...
app:layout_constraintStart_toEndOf="@+id/left_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now you are in your kotlin/java code, you have the following code:
val leftView = findViewById<View>(R.id.left_view)
Your cursor is on left_view
and you click on cmd + B
(Mac OS) to jump to your view inside the layout and BOOM! Android Studio asks you to choose toward which view id creation you want to go!
But which one is the one set in the android:id
of the view?
We use it when we want to create a new identifier for a view, it means when we write android:id="@+id/{VIEW_ID}"
.
The +
sign permits to create a new entry in the R.java
.
When we want to do a reference to a view, whose id already exists in R.java
.
In our previous example, we should use @id
to reference the left_view
in order to position view with id right_view
.
<View android:id="@+id/right_view"
...
app:layout_constraintStart_toEndOf="@id/left_view"/> <!-- No @+id here -->