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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With