[Q] TableLayout columns equal width

Search This thread

Auroratic

Member
Jun 15, 2013
48
0
Austria
Hello,

I want columns of equal width in a TableLayout, like this: http://androidadvice.blogspot.co.at/2010/10/tablelayout-columns-equal-width.html

but programmatically..

I create TextView's and add them to a TableRow, then I add the row to a TableLayout.

When I do the following:
Code:
tv.setLayoutParams(new TableLayout.LayoutParams(0, TableLayout.LayoutParams.WRAP_CONTENT, 1f));

the TextView's aren't visible.

How should I do that?
 
Last edited:

boggartfly

Senior Member
Dec 22, 2011
60
12
Madison
www.twitter.com
Did you set anything to the TextView? If you use this for eg:-

TextView txtview = (TextView) findbyViewId(r.id.txtview);

Then set it to
txtview.setText("Awesome");

Sent from my Nexus 4 using XDA Premium 4 mobile app
 

Auroratic

Member
Jun 15, 2013
48
0
Austria
That's the code:
Code:
table = (TableLayout) findViewById(R.id.timetable_tablelayout);
            for (int i2 = 1; i2 < 11; i2++) {

                // create a new TableRow
                TableRow row = new TableRow(this);

                for (int i = 0; i < 5; i++) {
                    String date = getKeyForIndex(i);

                    // create a new TextView
                    TextView t = new TextView(this);
                    t.setText(date);
                   
                    // set width, height and weight
                    t.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
// with this line, the textview's are not visible. when i comment the line, everything is ok

                    // add the TextView to the new TableRow
                    row.addView(t);
                }

                // add the TableRow to the TableLayout
                table.addView(row);
 
Last edited:

boggartfly

Senior Member
Dec 22, 2011
60
12
Madison
www.twitter.com
You should ideally be using
Code:
 new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

In instead of of what you are using... At least some variation of it. I haven't tried it but damn it should work! Let me know!

Sent from my Nexus 4 using XDA Premium 4 mobile app
 
Last edited:

warlock9_0

Member
Apr 22, 2013
35
9
Athens
ok, figured it out
you have to use the parents view layoutparams, so in your case you have to use TableRow

so just change to
Code:
 t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
 
  • Like
Reactions: Auroratic

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    ok, figured it out
    you have to use the parents view layoutparams, so in your case you have to use TableRow

    so just change to
    Code:
     t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));