tags:

views:

73

answers:

1

To use as an example: lets say that I have 2 EditTexts and one Button that I'm using as a login form. I want the EditTexts to be the same size, one after the other, with the login Button half their widths. Something like this:

example login screenshot

The only way that I've been able to find to make the button 1/2 the width (but still maintain it's dynamic sizing) is to use a TableLayout with an empty view as the first field. Something like this:

    <TableLayout
        android:stretchColumns="0,1"
        android:shrinkColumns="0,1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@id/login_EditText_password">
        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            <Button 
                android:text="@string/login_login" 
                android:id="@+id/login_Button_login" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>

That feels like a god-awful hack and there has GOT to be a better way. Do you know of one?

+2  A: 

Usually layout_weight is used to achieve percentage like behavior. But this is usually within a parent view. You could make an empty view to take up the other half in this situation, but it's a strange use case to want the button to be half the width of the above fields.

Edit, here's an example:

    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@id/login_EditText_password"
        android:orientation="horizontal">
        <View
            android:layout_width="0dip"
            android:layout_height="0dip"
            android:layout_weight="1" />
        <Button 
            android:text="@string/login_login" 
            android:id="@+id/login_Button_login" 
            android:layout_width="0dip" 
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
jqpubliq
`android:layout_weight` will only work within one row or column.
CommonsWare
A horizontal `LinearLayout` with an empty view of weight 1 and the button of weight 1 is essentially what this solution is doing but a little lighter and prettier.
jqpubliq
Yeah... that is a bit cleaner. I'm going to edit your answer to provide code. I can't believe I missed that.
fiXedd