Search code examples
androidandroid-tablelayout

Unable to make two views to occupy the screen equally


I am using the following code to create a table layout containing text view and spinner.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">

    <TableRow>

        <TextView
            android:id="@+id/lblListHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:text="Form"
            android:singleLine="true"
            android:textColor="#000" />
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:drawable/btn_dropdown"
            android:spinnerMode="dropdown"
            android:entries="@array/action"/>

    </TableRow>
</TableLayout>

The problem is that the width of both Text View and spinner are not equal. The size of spinner becomes small when the size of text view is big. How can i sort out this issue ?


Solution

  • Change the width of both of them to zero instead of the wrap_content.

    android:layout_width="0dp"
    

    When you're using weight and width at the same time, the width attribute will override the weight.