Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,

I am beginner in android development. In my application I was creating tablerow dynamically. I want to equally divide the table layout height to table rows. But its not working...

I am creating 9 rows each row contains 9 TextView

My xml is

HTML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/header"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"                                 
            android:orientation="vertical"  
            android:weightSum="100"   >

    <TableLayout 
        android:layout_weight="80"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:removed="#000"
        android:paddingTop="1dp"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:paddingBottom="1dp"
        android:id="@+id/sudokuboard"
        android:stretchColumns="*"
        android:weightSum="100" >

    </TableLayout> 


And my code is

Java
tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);

   //1st Row

   for(int i=0; i < 9; i++)
   {
       TableRow NewRow1 =new TableRow(this);
       NewRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT,10.0f));

       NewRow1.setPadding(1, 1, 1, 1);

       for(int j=0; j<9; j++)
       {
           TextView tv_00 = new TextView(this);
           int id=10*i;
           id=id+j;
           String strText=""+id;
           tv_00.setText(strText);
           tv_00.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT, 10.0f));
           tv_00.setGravity(Gravity.CENTER);
           tv_00.setBackgroundColor(Color.parseColor("#FFFFFF"));
           tv_00.setId(id);
           NewRow1.addView(tv_00);
       }
       //tblsudoku.addView(NewRow1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
       tblsudoku.addView(NewRow1);

   }
Posted

1 solution

Hi,

I have the exact same problem. :D

It seems, that the weight parameter in a TableRow is just not considered when adding the row to a TableLayout in code.

In a static XML-layout-file it works.


Update 1
I found the solution here!

The key sentence is "A widget must have the LayoutParams of its
*parent*."
, so you have to change the TableRow.LayoutParams to TableLayout.LayoutParams for your table rows.
Hope it works!
 
Share this answer
 
v2
Comments
Shanalal Kasim 3-Jan-14 4:43am    
Thanks... Its working
Shanalal Kasim 10-Jan-14 0:00am    
can you answer this question: http://www.codeproject.com/Questions/708827/How-to-set-dynamically-created-tablelayout-cell-he

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900