wotcha
wotcha

Reputation: 1

Problems with column width in Android Table

I'm trying to build a table that will let me list results from a search. I want the two columns to be different widths (e.g. 30% and 70%). I have got this working for the column titles:-

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="10dip"
    android:padding="10dp"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    android:textColor="@color/white" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:weightSum="1" >

        <TextView
            android:id="@+id/TextViewTitle1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="Date"
            android:background="@drawable/border"
            android:textColor="@color/bluish" />

        <TextView
            android:id="@+id/TextViewTitle2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:text="Task"
            android:background="@drawable/border"
            android:textColor="@color/bluish" />
    </TableRow>
</TableLayout>

I then want to add rows to the table in my program where the columns have the same width as the title row, but my code generates rows where the columns are split 50%-50% on width:-

private void BuildTable(int rows, int cols) {

       // outer for loop
       for (int i = 1; i <= rows; i++) {

        TableRow row = new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
          LayoutParams.WRAP_CONTENT));
        for (int j = 1; j <= cols; j++) {

         TextView tv1 = new TextView(this);
         tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
           LayoutParams.WRAP_CONTENT));
         tv1.setBackgroundResource(R.drawable.border);
         tv1.setPadding(5, 5, 5, 5);
         tv1.setText("R " + i + ", C" + j);



         row.addView(tv1);

        }

        table_layout.addView(row);

       }
      }

I'd appreciate any help on getting the column width to the same as the title.

Upvotes: 0

Views: 120

Answers (2)

Piyush
Piyush

Reputation: 1973

 private void addRow(Data data){

        TableRow row = (TableRow)View.inflate(getApplicationContext(), your table row , null);
       //find your textviews and set data                
        (TextView)row.findViewById(R.id.TextViewTitlei1);

     }
    }

they will be distributed according to your required weight.

Upvotes: 1

wotcha
wotcha

Reputation: 1

Well I've worked out what I needed to do this. Firstly set up a layout file, which I called table_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tr_row"

android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="1" >

 <TextView
                android:id="@+id/TextViewTitlei1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:text=" "
                android:background="@drawable/border"
                android:textColor="@color/bluish" />


            <TextView
                android:id="@+id/TextViewTitlei2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.7"
                android:text=" "
                android:background="@drawable/border"
                android:textColor="@color/bluish" />




</TableRow>

Then this is a simplified version of the code to make column widths 30% and 70%

private void addTableRow(String resIdTitle, String strValue){
         int yy=4;
         for (int i = 1; i <= yy; i++) {
         System.out.println("Step 16");   
         LayoutInflater inflater = getLayoutInflater();
            TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, table_layout, false);
            System.out.println("Step 17");
            // Add First Column
            TextView tvTitle = (TextView)tr.findViewById(R.id.TextViewTitlei1);
            tvTitle.setText(resIdTitle+ " "+i);
            System.out.println("Step 18");
            // Add the 3rd Column
            TextView tvValue = (TextView)tr.findViewById(R.id.TextViewTitlei2);
            tvValue.setText(strValue+ " "+i);
            System.out.println("Step 19");
            table_layout.addView(tr);
         }
        }

That appears to work.

Upvotes: 0

Related Questions