Amani
Amani

Reputation: 3979

Defining id ( @+id ) in constraint part of ConstraintLayout error

when first defining id ( @+id ) in constraint part ( such as app:layout_constraintBottom_toTopOf ) i get the error "cannot resolve symbol"

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:layout_constraintBottom_toTopOf="@+id/tv2" />

    <TextView
        android:id="@id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

see the screen shot:

enter image description here

In gradle I have constraint layout 1.1.3 and Android studio version is 3.2.1 I recently updated it from older version:

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

app runs correctly without any error but the error is shown in android studio layout window.

Invalidating caches, rebuilding, and changing constraint layout version NONE of them helps!

In Java class R.id.tv2 works correctly and when i control+click it I can see its field in R class. editor is using another class for ids that is not same as the R class in java code?


This question was about a bug in old Android Studio and ConstraintLayout versions in current versions of Android Studio using androidx library this bug is not presented.

Upvotes: 0

Views: 3475

Answers (3)

Kakyire
Kakyire

Reputation: 122

The plus sign + is added when defining the id (@+id/tv2) but when referencing you don't need to add the plus sign like this @id/tv2.

Instead of this

  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:layout_constraintBottom_toTopOf="@+id/tv2" />

    <TextView
        android:id="@id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

use this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:layout_constraintBottom_toTopOf="@id/tv2" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

To get it right I suggest you convert your code to Androidx.

The below code is written in Androidx

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/tv2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Screenshot

Upvotes: 2

Amani
Amani

Reputation: 3979

This question was about a bug in old Android Studio and ConstraintLayout versions in current versions of Android Studio using androidx library this bug is not presented.

Upvotes: -1

F1iX
F1iX

Reputation: 1013

As already mentioned in the other answers, use @+id/<your_name> to declare new resources.

In my case, Android Studio complained when using the - character in names (even though the app compiled and worked either way).

Upvotes: 1

Related Questions