praveenb
praveenb

Reputation: 10659

How to set padding to columns in table layout dynamically

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.

Upvotes: 2

Views: 17492

Answers (3)

FROSTYSMOOTH
FROSTYSMOOTH

Reputation: 77

Other solutions may work, but the easiest and best solution I have found so far is to add more "blank" columns where you need the spacing. If you are doing the buttons dynamically just add a text view between the buttons dynamically. I am only suggesting this if you are trying to use a TableLayout though.

Upvotes: 0

praveenb
praveenb

Reputation: 10659

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

Upvotes: 4

Kiril Kirilov
Kiril Kirilov

Reputation: 11257

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.

Upvotes: 3

Related Questions