Click here to Skip to main content
15,907,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am beginner in android. How to set dynamically created table layout cell height & width fixed.

Click here[^] for screenshot

The cell height & width was changing based on text size. i need all cell size as fixed.

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


Java Code

Java
Main = (LinearLayout) findViewById(R.id.main);

    Main_1 = new LinearLayout(this);
    LinearLayout.LayoutParams sudoku_1_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
    Main_1.setLayoutParams(sudoku_1_lp);
    Main_1.setWeightSum(100);
    Main_1.setOrientation(LinearLayout.VERTICAL);
    Main_1.setId(100);

    //Layout for sudoku_board 
    sudokuboard = new LinearLayout(this);
    LinearLayout.LayoutParams sudokuboard_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,0,80.0f);
    sudokuboard_lp.setMargins(1, 1, 1, 1);
    sudokuboard.setLayoutParams(sudokuboard_lp);
    sudokuboard.setWeightSum(80);
    sudokuboard.setOrientation(LinearLayout.VERTICAL);
    sudokuboard.setBackgroundColor(Color.parseColor("#FF0000"));
    sudokuboard.setId(102);

    tblsudoku = new TableLayout(this);
    TableLayout.LayoutParams sudoku_tbl_lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.FILL_PARENT,80.0f);
    sudoku_tbl_lp.setMargins(2, 2, 2, 2);
    tblsudoku.setLayoutParams(sudoku_tbl_lp);
    tblsudoku.setId(103);

    for(int i=0; i < 9; i++)
    {
            TableRow NewRow1 =new TableRow(this);

            TableLayout.LayoutParams n1 = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,0,10.0f);
            if(i==3 || i==6  )
            {
                n1.setMargins(0,2, 0, 0);
            }
            else
            {
                n1.setMargins(0,1, 0, 0);
            }

            NewRow1.setLayoutParams(n1);

            for(int j=0; j<9; j++)
            {
                TextView tv_00 = new TextView(this);
                int id=10*i;
                id=id+j;
                id=id*id*id;
                String strText=""+id;

                tv_00.setText(strText);
                TableRow.LayoutParams r1 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT, 10.0f);
                if(j==2 || j==5  )
                {
                    r1.setMargins(0, 0, 2, 0);
                }
                else
                {
                    r1.setMargins(0, 0, 1, 0);
                }

                tv_00.setLayoutParams(r1);
                tv_00.setGravity(Gravity.CENTER);                   
                tv_00.setBackgroundColor(Color.GREEN);
                tv_00.setId(id);
                tv_00.setPadding(2, 2, 2, 2);

                NewRow1.addView(tv_00);
            }
            tblsudoku.addView(NewRow1);
    }
    sudokuboard.addView(tblsudoku); 
    Main_1.addView(sudokuboard);
    Main.addView(Main_1);
Posted

Have you tried to set fixed value for the table layout?
Display display = getWindowManager().getDefaultDisplay();
	    Point outSize = new Point();
	    display.getSize(outSize);
	    TableLayout.LayoutParams sudoku_tbl_lp = new TableLayout.LayoutParams(outSize.x,outSize.y,80.0f);
 
Share this answer
 
First of all you do not need the weightSum attribute. Just give every row and cell the same weight, e.g. 1.
Then give every row a height of 0, and every cell (TextView) a width of 0.
 
Share this answer
 

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