Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enforce a circular reference in a RelativeLayout?

I have two widgets within a RelativeLayout that must reference each other. Technically it is not a circular reference since the widget A is vertically aligned with widget B and widget B is horizontally aligned with widget A. Here is my code (condensed):

    <Button android:id="@+id/btnLanguageFrom"
        android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/imgArrow"
            android:text="English" />

    <ImageView android:id="@+id/imgArrow"
        android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp" 
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@id/btnLanguageFrom"
            android:layout_centerHorizontal="true"
            android:src="@drawable/arrow_right" />

However when I build I get this error:

Error: No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/imgArrow').

Interestingly, the Graphical Layout view in Eclipse displays it correctly and doesn't complain about the circular reference.

I don't see the problem with two widgets referencing each other along different dimensions (horizontal and vertical) since it cannot cause an infinite loop. Is there any way around this problem? This is the only way I know to get the layout I need.

Thanks in advance, Barry P.S. Is there any way to declare an id in advance, like in C/C++?

like image 327
Barry Fruitman Avatar asked Apr 15 '11 19:04

Barry Fruitman


People also ask

Can we use linear layout inside RelativeLayout?

We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.

What is the default placement of objects in a RelativeLayout?

By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.

What is difference between relative and constraint layout?

Unlike RelativeLayout , ConstraintLayout offers a bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with a red circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.

Is constraint layout better than relative layout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI. Where in the Relative layout you needed multiple nested layouts (LinearLayout + RelativeLayout).


1 Answers

The first time you reference an ID, use the @+ prefix, which tells the resource builder to add the ID, rather than trying to find it. So try:

android:layout_toLeftOf="@+id/imgArrow"
like image 173
Jason LeBrun Avatar answered Sep 22 '22 11:09

Jason LeBrun