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++?
We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.
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.
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.
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).
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With