juan
juan

Reputation: 179

Android RelativeLayout and childs with layout_width="match_parent"

How Android calculates the size of a View when I have two Views at the same "row", one with width="fill_parent"?

Example:

<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent">  
    <EditText  
        android:id="@+id/EditText01"  
        android:hint="Enter some text..."  
        android:layout_alignParentLeft="true"  
        android:layout_width="match_parent"  
        android:layout_toLeftOf="@+id/Button01"  
        android:layout_height="wrap_content"></EditText>  
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here!"  
        android:layout_width="wrap_content"  
        android:layout_alignParentRight="true"  
        android:layout_height="wrap_content"></Button>  
</RelativeLayout>

This code gives all free space to EditText and show the button to his right. But with this changes the EditText fills all width and the button is out of the screen:

<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent">  
    <EditText  
        android:id="@+id/EditText01"  
        android:hint="Enter some text..."  
        android:layout_alignParentLeft="true"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"></EditText>  
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here!"  
        android:layout_width="wrap_content"  
    android:layout_toRightOf="@+id/EditText01"
        android:layout_height="wrap_content"></Button>  
</RelativeLayout

Upvotes: 4

Views: 3874

Answers (3)

bogdan
bogdan

Reputation: 782

If you are using a RelativeLayout then you have no "rows", all you have is a space where you can arrange the views. If you want to keep the button in the right part of the screen and have the EditText fill the rest of the screen you could do something like this:

<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent">  
    <EditText  
        android:id="@+id/EditText01"  
        android:hint="Enter some text..."  
        android:layout_toLeftOf="@+id/Button01"
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"></EditText>  
    <Button  
        android:id="@+id/Button01"  
        android:text="Press Here!" 
        android:alignParentRight="true"
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"></Button>  
</RelativeLayout/>

Upvotes: 0

TronicZomB
TronicZomB

Reputation: 8747

android:layout_toLeftOf="@+id/Button01" will force the right bound of the EditText to align to the left side of the Button , thus Android ignores the match_parent width forcing the width to only fill from the left parent side to the right side of the button.

android:layout_toRightOf="@+id/EditText01" will force the left bound of the Button to align to the right side of the EditText, but since the EditText is match_parent width the rights side is aligned to the right side of the parent view and Android will just force the button off the screen.

Upvotes: 7

Neoh
Neoh

Reputation: 16174

In both of your examples, <EditText/> fits all the width of screen. In the first example, the Button is not dependent on <EditText/> , so it can align to right edge of screen. In the second example, however, Button has to align to the right of `, that's why it gets pushed out of view.

Upvotes: 1

Related Questions