3

I am trying to center a textview inside a tablerow, that is itself inside a tablelayout, but I guess I am missing something, because the TextView doesn't get centered :s

Here is my method:

private void insertClock() {


        TableLayout table = new TableLayout(this);
        table.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        table.setStretchAllColumns(true);

        TableRow tbRow = new TableRow(this);
        TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);




        clock = new TextView(this);
        TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);  


        clock.setLayoutParams(layoutHistory);
        clock.setText(calculateClockText());
        tbRow.setLayoutParams(layoutRow);
        table.addView(tbRow);
        clock.setGravity(Gravity.CENTER_HORIZONTAL);
        tbRow.addView(clock);

    }

Thanks in advance ;)

TiagoM
  • 3,458
  • 4
  • 42
  • 83

1 Answers1

6

Finnaly made it working ! Here is the solution:

private void insertClock(TableLayout table, String text) {

        TableRow tbRow = new TableRow(this);
        TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);


        tbRow.setLayoutParams(layoutRow);

        clock = new TextView(this);
        TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);  
        clock.setLayoutParams(layoutHistory);
        clock.setText(text);


        clock.setGravity(Gravity.CENTER);
        tbRow.setGravity(Gravity.CENTER);
        tbRow.addView(clock);
        table.addView(tbRow);
    }
TiagoM
  • 3,458
  • 4
  • 42
  • 83