Eugene Chumak Eugene Chumak - 17 days ago 7
Android Question

layout_toRightOf works and layout_toLeftOf doesn't, how to solve?

I have simple relative layout - an ImageView (iv1) and a TextView (tv1) to the left of iv1. Unfortunately, I see no tv1. What is more, even hierarchyViewer can't help me find it.

<?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"
android:gravity="right"
android:background="@android:color/white"
>

<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/tv1"
android:layout_toLeftOf="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

</RelativeLayout>


It looks like that:

enter image description here

However, if I change android:layout_toLeftOf="@+id/iv1" to android:layout_toRightOf="@+id/iv1", my text view becomes positioned to right of image view. It seems like toRightOf works and toLeftOf doesn't.
Here how it looks:

enter image description here

What is the matter? How to make layout_toLeftOf work?

Answer

Change:

<TextView
    android:id="@+id/tv1"
    android:layout_toLeftOf="@+id/+iv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

to:

<TextView
    android:id="@+id/tv1"
    android:layout_toLeftOf="@id/iv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

You are using @+id/iv1 in your toLeftOf in the TextView instead of @id/iv1. Also you should give the imageview a specific place in the view. Try to align it: layout_parentTop = true