Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RelativeLayout children to left and right

I am trying to create a layout where there is a RelativeLayout and two child inside it. The two children are TextView and ImageView. I want the text to start from the very left of the RelativeLayout and ImageView to very right of the RelativeLayout.

What properties I need to use?

The code is which is not working.

<RelativeLayout 
    android:clickable="true"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:background="@drawable/android_btn_large" 
    android:gravity="center_vertical">

    <TextView 
            android:id="@+id/txtButton"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="Riverside Park" 
            android:textColor="#FFFFFF"
            android:layout_alignParentLeft="true">
    </TextView>
    <ImageView 
            android:id="@+id/imgButton"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/plus_icon_480">
     </ImageView>
</RelativeLayout>

The above works but stretch the button to fill_parent.

like image 429
Umair A. Avatar asked Dec 03 '22 07:12

Umair A.


2 Answers

that should do the job

<RelativeLayout 
    android:clickable="true"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:background="@drawable/android_btn_large"
    android:gravity="center_vertical">

<TextView 
    android:id="@+id/txtButton"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imgButton"
    android:text="Riverside Park"
    android:textColor="#FFFFFF"
    android:layout_alignParentLeft="true"></TextView>

<ImageView 
    android:id="@id/imgButton"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/txtButton"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/plus_icon_480">        
</ImageView>
</RelativeLayout>

notice that in TextView there is +id, and in ImageView there is no "+"". However you should use linear layout with weight set to "1" on both views.

like image 193
Mikooos Avatar answered Dec 05 '22 21:12

Mikooos


layout_alignparentleft="true" for the left hand child (TextView) and layout_alignparentright="true" for the right hand child (ImageView).

like image 39
Ian Newson Avatar answered Dec 05 '22 19:12

Ian Newson