How to set padding to columns in table layout dynamically


Solution 1

i found answer, by using setmargin to the layout params applied to button Set margins in a LinearLayout programmatically

LayoutParams layoutParams = new LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(30, 20, 30, 0);

Solution 2

You've been set padding of the edges of the TableRow, not between its elements.

You can set padding of every individual view in the TableRow by doing this:

for(int i = 0; i < tableRow.getVirtualChildCount(); i++){
    tableRow.getVirtualChildAt(i).setPadding(blockPadding, blockPadding, blockPadding, blockPadding);

This is really stupid, but it is the best I can think of right now.


Related videos on Youtube

Author by


Android Developer Programs Engineer @ PCCS

Updated on May 14, 2022


  • praveenb
    praveenb 15 days

    I am trying to create a table layout with buttons dynamically. I am able to get the table layout with buttons. bt i need padding between buttons. How i can get programatically.

    I tried following code bt

    private void showCowsTblField()
        for (int row = 0; row < numberOfRowsInField-1; row++)
            TableRow tableRow = new TableRow(this);  
            tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT ));
            for (int column = 0; column < numberOfColumnsInField -1; column++)
                blocks[row][column].setLayoutParams(new LayoutParams(  
                blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
                tableRow.setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
            tblCows.addView(tableRow,new TableLayout.LayoutParams(  

    Please let me know.... Thanks.

  • praveenb
    praveenb over 11 years
    I tried above code, bt not worked here. i donot understand the reason....:( anyway thnks for ur reply ..fiction