FORUMS

Google No Longer Sending Calendar SMS Notifications

In a not entirely surprising move, Google announced that it’s putting an … more

Enable Multi-Window Mode on M Developer Preview

What was not mentioned in yeterday’s keynote was Android M’s multi-window … more

I/O Summary: Google Cardboard Virtual Reality

One year ago, Google introduced cardboard. Amazingly enough, that was all it took to fire … more

Android M Preview Images – XDA TV

Android M preview images are available. That and much more news is covered by Jordan when he … more

[Q] TableLayout columns equal width

Thanks Meter: 0
 
By Auroratic, Member on 7th February 2014, 12:25 PM
Post Reply Subscribe to Thread Email Thread
Hello,

I want columns of equal width in a TableLayout, like this: http://androidadvice.blogspot.co.at/...ual-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 by Auroratic; 9th February 2014 at 08:07 PM.
 
 
8th February 2014, 06:11 PM |#2  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
More
Anyone?
8th February 2014, 07:12 PM |#3  
boggartfly's Avatar
Member
Flag Mumbai
Thanks Meter: 11
 
Donate to Me
More
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
8th February 2014, 08:53 PM |#4  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
More
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 by Auroratic; 10th February 2014 at 06:02 PM.
9th February 2014, 04:50 AM |#5  
boggartfly's Avatar
Member
Flag Mumbai
Thanks Meter: 11
 
Donate to Me
More
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 by boggartfly; 9th February 2014 at 04:53 AM.
9th February 2014, 07:44 PM |#6  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
More
Im already using that..
Quote:

// set width, height and weight
t.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams. FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));

10th February 2014, 11:07 AM |#7  
warlock9_0's Avatar
Member
Flag Athens
Thanks Meter: 9
 
More
have you tried setting width to 0 and weight to 1?
width must be 0 for weight to work
Last edited by warlock9_0; 10th February 2014 at 11:11 AM. Reason: added explanation
10th February 2014, 11:40 AM |#8  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
More
yes, then I get an 'java.lang.ArithmeticException: divide by zero' exception
Last edited by Auroratic; 10th February 2014 at 11:51 AM.
10th February 2014, 03:05 PM |#9  
warlock9_0's Avatar
Member
Flag Athens
Thanks Meter: 9
 
More
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));
The Following User Says Thank You to warlock9_0 For This Useful Post: [ View ]
10th February 2014, 05:53 PM |#10  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
More
Thumbs up thank you!
Quote:
Originally Posted by warlock9_0

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

Thank you, it's working now!!
Last edited by Auroratic; 10th February 2014 at 06:04 PM.
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes