HughHughTeotl
HughHughTeotl

Reputation: 5789

Android RelativeLayout with wrap_content too small to fit children

Here is a simple RelativeLayout with 4 controls arranged in a tabular format. When displayed, the confirmpassword box is clipped at the bottom. It's as if "wrap_content" is not correctly calculating the height of the children. Any ideas?

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >  
    <TextView
        android:id="@+id/passwordlabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="34dp"
        android:text="Password"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/passwordlabel"
        android:layout_alignLeft="@+id/confirmpassword"
        android:layout_toRightOf="@id/passwordlabel"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/confirmpasslabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/passwordlabel"
        android:layout_below="@id/passwordlabel"
        android:text="Confirm Password"
        android:layout_marginTop="34dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/confirmpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/confirmpasslabel"
        android:layout_toRightOf="@id/confirmpasslabel"
        android:layout_marginLeft="15dp"
        android:ems="10"
        android:inputType="textPassword" />
</RelativeLayout>

This is what it looks like:

RelativeLayout in emulator

I have tried adding padding and/or margins to both of the bottom controls which has no effect (and strikes me as a hack anyway).

NB:- I had a similar problem before which a kindly soul solved essentially by splitting apart the RelativeLayout. I don't think that's possible here (it may be) but I'm concerned I'm either doing something completely wrong, or the RelativeLayout has a massive bug which seems under-documented.

Anyone able to shed light on this? TIA!

Upvotes: 0

Views: 1324

Answers (1)

Ken Wolf
Ken Wolf

Reputation: 23269

It does seem like a bit of a bug. I couldn't get it to work properly (without changing it somehow) with a RelativeLayout - something with the margins was throwing it off.

As a workaround, try a TableLayout:

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TableRow android:layout_marginTop="34dp">

        <TextView
            android:id="@+id/passwordlabel"
            android:text="Password"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/password"
            android:ems="10"
            android:inputType="textPassword" >

            <requestFocus />
        </EditText>

    </TableRow>

    <TableRow android:layout_marginTop="34dp">

        <TextView
            android:id="@+id/confirmpasslabel"
            android:text="Confirm Password"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/confirmpassword"
            android:layout_marginLeft="15dp"
            android:ems="10"
            android:inputType="textPassword" />

    </TableRow>

</TableLayout>

Upvotes: 1

Related Questions