Reputation: 198
I have a relative layout in which I have a TextView
on the Left and a Spinner on the right. Also I need an error image to show if user selected incorrect variant.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="@string/some_text"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/err"/>
<ImageView
android:id="@+id/err"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_error"/>
</RelativeLayout>
I want my spinner to be on the aligned to the right when error is not shown (visibility = GONE) and move it to be to the left of error image when the error is visible. How do I do that? Now it just ignores this:
android:layout_toLeftOf="@+id/err"
EDIT: thank you, I corrected the typo, but it's not the cause of the problem
Upvotes: 3
Views: 3904
Reputation: 855
Perhaps you want something like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:layout_alignParentRight="true"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp" />
<ImageView
android:id="@+id/err"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_error"/>
</LinearLayout>
EDIT: Just put your Spinner
and ImageView
inside a LinearLayout
like this one.
EDIT 2: I just received a +10
and as I am a bit ashamed about this solution, follow this:
So, for future questions related to this one, please ignore my answer and create your own custom view
extending ConstraintLayout
and include a Spinner
and an ImageView
on the custom layout, preferably using Kotlin. Thanks!
Upvotes: 5
Reputation: 2462
Adding an anchor view on the right side of the relative layout can solve this issue. Add the anchor view to the RelativeLayout:
<View
android:id="@+id/anchor_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
Replace the error ImageView relative position from android:layout_alignParentRight="true"
to android:layout_toLeftOf="@id/anchor_top_right"
. Also remove android:layout_alignParentRight="true"
from the Spinner, it's aligned to the left of the err.
Here's the full layout with the anchor:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<View
android:id="@+id/anchor_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="@string/some_text"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="74dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/err"
/>
<ImageView
android:id="@+id/err"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/anchor_top_right"
android:src="@drawable/ic_error"
/>
</RelativeLayout>
Upvotes: 3
Reputation: 693
I think you have a typo in your code.
It has to be android:layout_toLeftOf="@+id/err"
instead of android:layout_toLeftOf="@id/err"
Upvotes: 0