0

I have tablelayout with rows and in each row a bunch of textviews (all added programatically, no xml), I want to have gridlines so I am trying to add margins through laoyoutparams but it looks like TableLayout.LayoutParams doesn't apply to textviews only to rows so I am getting a margin on the rows but not on the cells. I have also tried using RelativeLaout.LayoutParams on the cells but thaqt is not working also. Anyone can propose any solution for my problem?

Adnan Pirota
  • 299
  • 5
  • 25

1 Answers1

1

You should use correct LayoutParams for views. While adding a view to:

  1. TableLayout; use TableLayout.LayoutParams
  2. TableRow; use TableRow.LayoutParams.

Edit: I've edited the code. You want some black spaces for some cells. You can achive this with setting same bg color to that views(or transparent bg for row and views on cell). I think setting same bg color to textViews with tableLayout is easier.

Here is a complete code sample that you can try(colors are added to see output easily):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TableLayout tableLayout = createTableLayout(16, 14);
    setContentView(tableLayout);

    makeCellEmpty(tableLayout, 1, 6);
    makeCellEmpty(tableLayout, 2, 3);
    makeCellEmpty(tableLayout, 3, 8);
    makeCellEmpty(tableLayout, 3, 2);
    makeCellEmpty(tableLayout, 4, 8);
    makeCellEmpty(tableLayout, 6, 9);

}

public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
    // get row from table with rowIndex
    TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);

    // get cell from row with columnIndex
    TextView textView = (TextView)tableRow.getChildAt(columnIndex);

    // make it black
    textView.setBackgroundColor(Color.BLACK);
}

private TableLayout createTableLayout(int rowCount, int columnCount) {
    // 1) Create a tableLayout and its params
    TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setBackgroundColor(Color.BLACK);

    // 2) create tableRow params
    TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
    tableRowParams.setMargins(1, 1, 1, 1);
    tableRowParams.weight = 1;

    for (int i = 0; i < rowCount; i++) {
        // 3) create tableRow
        TableRow tableRow = new TableRow(this);
        tableRow.setBackgroundColor(Color.BLACK);

        for (int j= 0; j < columnCount; j++) {
            // 4) create textView
            TextView textView = new TextView(this);
            textView.setText(String.valueOf(j));
            textView.setBackgroundColor(Color.WHITE);
            textView.setGravity(Gravity.CENTER);

            // 5) add textView to tableRow
            tableRow.addView(textView, tableRowParams);
        }

        // 6) add tableRow to tableLayout
        tableLayout.addView(tableRow, tableLayoutParams);
    }

    return tableLayout;
}

And here is the output for this code that we can see margins are applied correctly:
enter image description here

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • Thank you aegean what if I need margin between textView1 and textView 2 (to have some blue area) ? – Adnan Pirota Nov 10 '13 at 22:03
  • You can add margins to tableRowparams: tableRowParams.setMargins(10, 20, 10, 20); – Devrim Nov 10 '13 at 22:05
  • It's logical but it's not working on my setup, my row has like 13 textviews and some buttons, so I actually want to have normal grid but nothing is working. I have read somewhere that you need to set it for buttons because they represent columns, but don't know how to set that also – Adnan Pirota Nov 10 '13 at 22:12
  • Sorry I couldn't understand. In tableLayout you have tableRows. If you put a view directly to tableRow, it will be added horizontally in tableRow. If you want something different use gridLayout. Share what you've done, your code. or share what you want as an image maybe to be more clear. – Devrim Nov 10 '13 at 22:25
  • My TableLayout consists of 16 TableRows and in each row are 13 or more columns (my app is crosswords) so basically for my TableLayout I have set black color as background and for my TextViews in TableRows I have set white color background so I need margin between texviews so that layout appear as grid – Adnan Pirota Nov 10 '13 at 22:37
  • 1
    See my edited answer. I guess you'll get the point and manage the code for your needs. – Devrim Nov 10 '13 at 23:49
  • Correct. My mistake was that I was not applying tableRowParams on textViews. Thank you very much for your assistance – Adnan Pirota Nov 11 '13 at 18:12