How to set padding to columns in table layout dynamically
17,276
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
praveenb
Android Developer Programs Engineer @ PCCS http://www.praveenboyalapalli.com/
Updated on June 04, 2022Comments
-
praveenb over 1 year
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( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding); tableRow.addView(blocks[row][column]); tableRow.setPadding(blockPadding, blockPadding, blockPadding, blockPadding); } tblCows.addView(tableRow,new TableLayout.LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); } }
Please let me know.... Thanks.
-
praveenb almost 13 yearsI tried above code, bt not worked here. i donot understand the reason....:( anyway thnks for ur reply ..fiction