Jason
Jason

Reputation: 6926

RelativeLayout height 'wrap_content' does not wrap content

I'm making a layout to replicate a custom dialog. For the bottom portion of my dialog, the RelativeLayout hosting the positive, neutral, and negative buttons has an extended height even though I set the RelativeLayout height to wrap_content.

My layout XML is below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/TextView_Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:layout_marginBottom="10dip"
    android:layout_marginLeft="0dip"
    android:layout_marginRight="0dip"
    android:layout_marginTop="10dip"
    android:background="@android:drawable/divider_horizontal_dim_dark" >
</LinearLayout>

<TextView
    android:id="@+id/TextView_Message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:text="TextView" />

<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/bottom_bar" >

    <Button
        android:id="@+id/Button_Positive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/Button_Neutral"
        android:layout_centerVertical="true"
        android:text="Button" />

    <Button
        android:id="@+id/Button_Neutral"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button" />

    <Button
        android:id="@+id/Button_Negative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/Button_Neutral"
        android:layout_centerVertical="true"
        android:text="Button" />
</RelativeLayout>

Java Activity code:

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);

        // Assign content references
        TextView message = (TextView) findViewById(R.id.TextView_Message);
        message.setText("Are you sure you want to sign out?");
    }
}

Upvotes: 0

Views: 4968

Answers (1)

MH.
MH.

Reputation: 45493

I didn't have any problems rendering the layout correctly in Eclipse (as commented earlier), or on a Motorola Defy MB525. However, if I would set the activity's theme to Theme.Dialog, it was showing up squeezed length-wise, resulting in only the neutral button being visible. There are also some simplifications you could do to the layout as presented in your question, e.g. unnecessary nesting of the title TextView and (ab)using a LinearLayout for a horizontal divider.

I changed some small things resulting in the layout below. Perhaps you can give that a try?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TextView_Title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:gravity="center_horizontal"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <View
        android:id="@+id/horizontal_divider3"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="0dip"
        android:layout_marginRight="0dip"
        android:layout_marginTop="10dip"
        android:background="@android:drawable/divider_horizontal_bright" />

    <TextView
        android:id="@+id/TextView_Message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:text="TextView" />

    <LinearLayout
        android:id="@+id/button_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/bottom_bar" 
        android:gravity="center">

        <Button
            android:id="@+id/Button_Positive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/Button_Neutral"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/Button_Negative"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

</LinearLayout>

By the way, in stead of using an activity to emulate a dialog, you could also just use this layout for a 'real' dialog. There's a good example on creating custom dialogs on the Android dev site.

Upvotes: 1

Related Questions