How to set Table Column in android

34,077

The thing you have already written should actually already create two columns, the thing is that they might not situate as you expect on the screen - the columns will be as narrow as possible. TableLayout tag in the Android layout has several attributes. One of them is the stretch columns - if given the described columns will be stretched so that to fill all the designated width. If you need all of them stretched evenly use star, if you want any specific column to be stretched covering the remaining space use its 1 based index (you can specify a group of indices). See here:

<TableLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0,1" >
   <TableRow android:layout_width="fill_parent"> 
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="ok"    
      />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="bye" />
   </TableRow>
</TableLayout>

By the way if you need just a single row you might be able to do the same with LinearLayout and orientation="horizontal". If you have several rows, keep in mind that you are really dealing with table - all rows of a column will be situated exactly one above the other and the widest row will determine the width of the column.

Share:
34,077
abhishek ameta
Author by

abhishek ameta

Updated on November 20, 2020

Comments

  • abhishek ameta
    abhishek ameta over 3 years

    I am having some difficulty setting layout parameters of table rows (containing text views).

    I want to add some columns to get a good layout. I am doing it dynamically. (in code)

    <TableRow> 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ok"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bye"/>
    </TableRow>
    

    I want these two textviews to become two columns and layout on screen accordingly.