androidandroid-tablelayout

layout_width attribute of TableLayout children


While going through the Android developer docs for TableLayout , I saw a line mentioning

The children of a TableLayout cannot specify the layout_width attribute."

But in code if I use the layout_width attribute with TextView, the width gets increased accordingly.

Am I missing something?

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:stretchColumns="*"
>

<TableRow android:padding="5dip">

    <TextView
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="loginForm"
        android:textColor="#0ff"
        android:textSize="25sp"
        android:textStyle="bold" />
</TableRow>

<TableRow>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="200dp"
        android:layout_marginLeft="10dp"
        android:text="User Name"
        android:textColor="#fff"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/userName"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="#fff"
        android:padding="5dp"
        android:textColor="#000" />

</TableRow>

<TableRow>

    <TextView
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:text="password"
        android:textColor="#fff"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/password"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:background="#fff"
        android:padding="5dp"
        android:textColor="#000" />

</TableRow>

</TableLayout>

Solution

  • I believe it is talking about the first level child component inside the TableLayout component.

    If you add a TextView at the same level as your TableRow, the width will cannot be adjusted.

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical"
    android:stretchColumns="*">
    
    <!--This TextView will fail to adjust its width-->
    <TextView
          android:text="123"/>
    
    <TableRow android:padding="5dip">
        <TextView
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:text="loginForm"
            android:textColor="#0ff"
            android:textSize="25sp"
            android:textStyle="bold" />
    </TableRow>
    
    ...
    
    </TableLayout>`