Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q] TableLayout columns equal width

OP Auroratic

7th February 2014, 01:25 PM   |  #1  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
45 posts
Join Date:Joined: Jun 2013
More
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 09:07 PM.
8th February 2014, 07:11 PM   |  #2  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
45 posts
Join Date:Joined: Jun 2013
More
Anyone?
8th February 2014, 08:12 PM   |  #3  
boggartfly's Avatar
Member
Flag Mumbai
Thanks Meter: 11
 
59 posts
Join Date:Joined: Dec 2011
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, 09:53 PM   |  #4  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
45 posts
Join Date:Joined: Jun 2013
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 07:02 PM.
9th February 2014, 05:50 AM   |  #5  
boggartfly's Avatar
Member
Flag Mumbai
Thanks Meter: 11
 
59 posts
Join Date:Joined: Dec 2011
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 05:53 AM.
9th February 2014, 08:44 PM   |  #6  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
45 posts
Join Date:Joined: Jun 2013
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, 12:07 PM   |  #7  
warlock9_0's Avatar
Member
Flag Athens
Thanks Meter: 9
 
32 posts
Join Date:Joined: Apr 2013
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 12:11 PM. Reason: added explanation
10th February 2014, 12:40 PM   |  #8  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
45 posts
Join Date:Joined: Jun 2013
More
yes, then I get an 'java.lang.ArithmeticException: divide by zero' exception
Last edited by Auroratic; 10th February 2014 at 12:51 PM.
10th February 2014, 04:05 PM   |  #9  
warlock9_0's Avatar
Member
Flag Athens
Thanks Meter: 9
 
32 posts
Join Date:Joined: Apr 2013
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, 06:53 PM   |  #10  
Auroratic's Avatar
OP Member
Flag Austria
Thanks Meter: 0
 
45 posts
Join Date:Joined: Jun 2013
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 07:04 PM.

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes