Search code examples
androidcentertablelayoutmultiple-columns

Center one Column in a Tablelayout and align the other to the right and left


I just started with some Android Programming and have a really hard time with making layouts. I want to make the following tablelayout and I want to have the middle TextView (textView3) centered horizontally of the Page and the other two TextViews should be to the left and right of the centered column. If one of the values in the left or right TextView generates a line break the other two TextViews should also be centered vertically.

I hope someone understands my problem. Greetz

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </TableRow>

Solution

  • Okay, after some more research I got it working by setting the android:layout_width="0dip" for each TextView. The Problem was, that android:layout_weight depends on the actual width of the TextViews, so "wrap_content" was not working.

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    
        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </TableRow>