Search code examples
androidtextviewandroid-relativelayout

Android Relative Layout add Margin when Align Parent


I have a relative layout in my Android view.

I am trying to get the middle text to have some right padding or margin based on the EDIT icon. See below the image I'm going for.

However I can't seem to get the middle text to perform. It runs all the way up to the EDIT button.

What am I doing wrong here?

enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:layout_height="30dp"
    android:background="#26265e"
    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="14dp"
        android:layout_height="14dp"
        android:src="@drawable/ic_my_location_white_18dp"

        android:layout_alignParentLeft="true"
        android:adjustViewBounds="true"
        android:ellipsize="middle"
        android:layout_marginLeft="12dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="8dp"

        />

    <TextView
        android:id="@+id/tiLocationAddress"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textColor="#ffffff"
        android:ellipsize="middle"
        android:maxLines="1"
        android:layout_marginBottom="7dp"
        android:layout_marginLeft="30dp"
        android:textSize="12dp"

        />


    <TextView
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textColor="#ffffff"
        android:textSize="13dp"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:text="Edit"
        android:onClick="onClick"
        android:clickable="true"

        />

</RelativeLayout>

Solution

  • You need to say layout_toRightOf for your text view:

    <TextView
        android:id="@+id/tiLocationAddress"
        //android:layout_alignParentLeft="true" - remove this
        android:layout_toRightOf="@id/imageView1" - add this
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textColor="#ffffff"
        android:ellipsize="middle"
        android:maxLines="1"
        android:layout_marginBottom="7dp"
        android:layout_marginLeft="30dp"
        android:textSize="12dp"
    
        />