Shanalal Kasim
Shanalal Kasim

Reputation: 131

Android: How to equally divide the table layout height to table rows in programmatically

I am creating tablerow (9 rows) 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

<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:background="#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

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);

    }

Please help me to solve this issue...

Updation on 29/12/2013

New Code

        tblsudoku = (TableLayout) findViewById(R.id.sudokuboard);
    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,1f));
        NewRow1.setBackgroundColor(Color.RED);

        LinearLayout Newrowlayout =new LinearLayout(this);
        Newrowlayout.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
        Newrowlayout.setOrientation(1);

        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 LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT, 1f));
            tv_00.setGravity(Gravity.CENTER);
            tv_00.setTextColor(Color.YELLOW);
            tv_00.setId(id);
            Newrowlayout.addView(tv_00);
        }
        NewRow1.addView(Newrowlayout);
        tblsudoku.addView(NewRow1);

    }

Design

 <TableLayout   xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"                    
            android:background="#000"
            android:paddingTop="1dp"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:paddingBottom="1dp"
            android:id="@+id/sudokuboard">
         </TableLayout>

Upvotes: 1

Views: 2312

Answers (2)

Shanalal Kasim
Shanalal Kasim

Reputation: 131

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!

More details click Here

Upvotes: 4

Adnan Mulla
Adnan Mulla

Reputation: 2866

Next time use the search bar at the top right corner before posting a question pls. Have a look at the thread below, I believe that is what your looking for.

Table Layout with equal rows height

Upvotes: -1

Related Questions