1

I'm trying to centre two textviews vertically using constraint layout chains. I know I can wrap the text layouts in a linear layout but I wanted to try out the new chain feature. When I build my project though I get an error, Error:(28, 50) No resource found that matches the given name (at 'layout_constraintBottom_toTopOf' with value '@id/item_school_location').

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="72dp">

        <ImageView
            android:id="@+id/item_school_logo"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginStart="16dp"
            android:contentDescription="@string/item_school_logo_description"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.5" />

        <TextView
            android:id="@+id/item_school_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="72dp"
            android:maxLines="1"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Subtitle"
            app:layout_constraintBottom_toTopOf="@id/item_school_location"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.5"
            app:layout_constraintVertical_chainPacked="true" />

        <TextView
            android:id="@+id/item_school_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="72dp"
            android:maxLines="1"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/item_school_name" />
    </android.support.constraint.ConstraintLayout>
</layout>

Anyone know what's causing this error?

Vinay Nagaraj
  • 1,162
  • 14
  • 26

2 Answers2

5
app:layout_constraintLeft_toLeftOf="parent"

Maybe I'm not completely up to date, but I think the value shouldn't be "parent" - it should be a view ID (in the format of "@+id/some_id").

Give your constraint layout an id and then try using that instead of parent.

Edit: I guess I am out of date on this? Because I just read that again, and your error seems like this line:

layout_constraintBottom_toTopOf="@id/item_school_location"

is missing the + after @ - as in it should be

layout_constraintBottom_toTopOf="@+id/item_school_location"
Zenon Seth
  • 201
  • 1
  • 3
  • Hah! I had a similar issue. I didn't expect this to work, but it did. I suppose the first reference to the ID (from the top) should have the + sign. – amram99 Feb 23 '17 at 02:02
  • @amram99 - as far as I know, all references to an id in a layout file should have the + in the front. Try it out, I'm fairly certain that even if your reference is the 2nd occurrence of an id, it still won't work right without the + – Zenon Seth Feb 23 '17 at 08:19
  • 1
    actually the + indicates that you're assigning a new ID. Normally you would reference an ID without a + (such as in a RelativeLayout, but Android is forgiving about assigning the same ID multiple times. Check out this answer - http://stackoverflow.com/a/5731533/389643. But since Android uses the order of elements to define the z-order, sometimes you have to reference an ID above the definition of the element, and sometimes that causes the compiler problems. – amram99 Feb 23 '17 at 15:13
1

I think the

No resource found that matches the given name

error is due to the fact that you are referencing the id of an element (item_school_location) that is declared after the element referencing it (item_school_name).

In this case, it would help if you declare the ids of your related elements in a separate ids.xml file (check this: https://stackoverflow.com/a/12247971)

Community
  • 1
  • 1
iluz
  • 43
  • 5