Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Circular Dependencies in Relative Layout

I am trying to run my code, a compiler error comes up saying that you cannot have circular dependencies in the relative layout.
Also, because of this error it does not let me look at the display view of the relative layout. Some forums online say that it is due to the positioning of the relative layout objects to one another, however I do not know if that relates to my code. My code to the relative layout is pasted below, if someone could let me know what the circular dependencies is referring to I would greatly appreciate it.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="#FFFF00" android:id="@+id/relativeLayout">

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Rock"
    android:id="@+id/rockButton"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="24sp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Paper"
    android:id="@+id/paperButton"
    android:textSize="24sp"
    android:layout_above="@+id/lizardButton"
    android:layout_toRightOf="@+id/textView3"
    android:layout_toEndOf="@+id/textView3" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Scissors"
    android:id="@+id/scissorsButton"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textSize="24sp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Lizard"
    android:id="@+id/lizardButton"
    android:layout_toStartOf="@+id/paperButton"
    android:layout_marginTop="44dp"
    android:layout_below="@+id/paperButton"
    android:layout_toLeftOf="@+id/paperButton"
    android:textSize="24sp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Spock"
    android:id="@+id/spockButton"
    android:layout_alignBottom="@+id/lizardButton"
    android:layout_toRightOf="@+id/paperButton"
    android:layout_toEndOf="@+id/paperButton"
    android:textSize="24sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/gameResult"
    android:layout_marginBottom="44dp"
    android:textSize="28sp"
    android:layout_above="@+id/paperButton"
    android:layout_alignLeft="@+id/lizardButton"
    android:layout_alignStart="@+id/lizardButton"
    android:layout_alignRight="@+id/spockButton"
    android:layout_alignEnd="@+id/spockButton"
    android:layout_alignParentEnd="false"
    android:textAlignment="center"
    android:singleLine="false" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="User's Choice"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="20sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Computer's Choice"
    android:id="@+id/textView4"
    android:layout_alignTop="@+id/textView3"
    android:layout_alignRight="@+id/scissorsButton"
    android:layout_alignEnd="@+id/scissorsButton"
    android:textSize="20sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView5"
    android:layout_below="@+id/textView3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="35dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView6"
    android:layout_alignTop="@+id/textView5"
    android:layout_alignRight="@+id/textView4"
    android:layout_alignEnd="@+id/textView4" />

</RelativeLayout>

1 Answer

Hi Chuck

I think the error relates to your LizardButton and paperButton. you state the PaperButton should be above the lizard button which means the layout looks to the lizardButton for it's place and tries to put the PaperButton above it. However at the same time the LizardButton is looking for the paperButton's place and trying to put the lizardButton below the paperButton. At this point the complier is at a stalemate as neither layout can be positioned without the other being positioned first.

Try editing your layout to place items relative to the screen first or parent view, it may be easier to do this in XML.

Hope this helps

Daniel

Thanks Daniel, that did help a lot!