2 12 Votes

Android Table Layout with columns of equal width

Tutorial by Stefan Trost | 25/09/2013 at 15:03

Today, I would like to show you a possibility of how to design an XML Table Layout for Android with columns of the same width.

The key lies in the specifications layout_width="0dip" and layout_weight="1". In the following example, we have three columns with the same width containing a TextView:

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

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" >

      <!-- Column 1 -->
      <TextView
         android:id="@+id/textView1"
         android:layout_height="wrap_content"
         android:layout_width="0dip"
         android:layout_weight="1"
         android:text="Column 1" />
         
      <!-- Column 2 -->
      <TextView
         android:id="@+id/textView2"
         android:layout_height="wrap_content"
         android:layout_width="0dip"
         android:layout_weight="1"
         android:text="Column 2" />
         
      <!-- Column 3 -->
      <TextView
         android:id="@+id/textView3"
         android:layout_height="wrap_content"
         android:layout_width="0dip"
         android:layout_weight="1"
         android:text="Column 3" />
    </TableRow>

    <!-- more rows
    <TableRow ... </TableRow>
    -->
</TableLayout>

You can add more rows by copying and editing the TableRow section from "<TableRow" to "</TableRow>" to the commented part  "more rows".

ReplyPositiveNegative

About the Author

Avatar AuthorYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile | Message

 

Related Topics

HTML: Table - Merge Cells
Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.