5,604,443 Members 48,476 Now Online
XDA Developers Android and Mobile Development Forum

[Q] TableLayout columns equal width

Tip us?
 
Auroratic
Old
(Last edited by Auroratic; 9th February 2014 at 08:07 PM.)
#1  
Auroratic's Avatar
Junior Member - OP
Thanks Meter 0
Posts: 29
Join Date: Jun 2013
Location: Austria
Default [Q] TableLayout columns equal width

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?
 
Auroratic
Old
#2  
Auroratic's Avatar
Junior Member - OP
Thanks Meter 0
Posts: 29
Join Date: Jun 2013
Location: Austria
Anyone?
 
boggartfly
Old
#3  
boggartfly's Avatar
Member
Thanks Meter 9
Posts: 51
Join Date: Dec 2011
Location: Mumbai

 
DONATE TO ME
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
Old
(Last edited by Auroratic; 10th February 2014 at 06:02 PM.)
#4  
Auroratic's Avatar
Junior Member - OP
Thanks Meter 0
Posts: 29
Join Date: Jun 2013
Location: 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);
 
boggartfly
Old
(Last edited by boggartfly; 9th February 2014 at 04:53 AM.)
#5  
boggartfly's Avatar
Member
Thanks Meter 9
Posts: 51
Join Date: Dec 2011
Location: Mumbai

 
DONATE TO ME
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
 
Auroratic
Old
#6  
Auroratic's Avatar
Junior Member - OP
Thanks Meter 0
Posts: 29
Join Date: Jun 2013
Location: Austria
Im already using that..
Quote:
// set width, height and weight
t.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams. FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
 
warlock9_0
Old
(Last edited by warlock9_0; 10th February 2014 at 11:11 AM.) Reason: added explanation
#7  
warlock9_0's Avatar
Junior Member
Thanks Meter 6
Posts: 22
Join Date: Apr 2013
Location: Athens
have you tried setting width to 0 and weight to 1?
width must be 0 for weight to work
 
Auroratic
Old
(Last edited by Auroratic; 10th February 2014 at 11:51 AM.)
#8  
Auroratic's Avatar
Junior Member - OP
Thanks Meter 0
Posts: 29
Join Date: Jun 2013
Location: Austria
yes, then I get an 'java.lang.ArithmeticException: divide by zero' exception
 
warlock9_0
Old
#9  
warlock9_0's Avatar
Junior Member
Thanks Meter 6
Posts: 22
Join Date: Apr 2013
Location: 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));
The Following User Says Thank You to warlock9_0 For This Useful Post: [ Click to Expand ]
 
Auroratic
Old
(Last edited by Auroratic; 10th February 2014 at 06:04 PM.)
#10  
Auroratic's Avatar
Junior Member - OP
Thanks Meter 0
Posts: 29
Join Date: Jun 2013
Location: Austria
Thumbs up thank you!

Quote:
Originally Posted by warlock9_0 View Post
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!!

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


XDA PORTAL POSTS

Leak Points to Windows Start Menu’s Return as Soon as August

About a month ago at this year’s Build conference, Microsoft … more

Wearables Walkthrough: Tasker on the Samsung Galaxy Gear – XDA Developer TV

The future certainly looks like it is full of wearables. … more

WinDroid HTC Toolkit Lets You Root, Unlock, and Flash ROMs on Various HTC Devices

Plenty of users nowadays have more than one mobile device. … more

When Using GitHub, Don’t Skimp on Commit History

GitHub has become the epicenter of most open source development work that is posted … more