Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 822
juan Avatar asked Feb 17 '23 13:02

juan


2 Answers

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.

like image 152
TronicZomB Avatar answered Apr 06 '23 14:04

TronicZomB


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.

like image 29
Neoh Avatar answered Apr 06 '23 13:04

Neoh