0

Alright, I have searched for its solution But unable to find it. The Question is simple. There is a TableLayout in Android, I am adding few Rows in it. In the Last Row, I am adding a button. Now, the problem is the size of the button is not constant. What I mean is that, it fills the column. If the above data takes longer width of the column, the width of the button, accordingly increases. I have tried LayoutParams, setHeight, setWidth function too, but that didn't help me..

I would appreciate if someone would help me out.

TableLayout table=new TableLayout(this);
TableRow tableRow=new TableRow(this);
Button button=new Button(this);
TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 
Button.setLayoutParams(trParams); 
tableRow.AddView(button); 
table.addView(tableRow);
Sound Conception
  • 5,263
  • 5
  • 30
  • 47
  • Share your xml file.... – Namrata Feb 13 '14 at 05:07
  • I am doing this dynamically i.e. programmitically. – user3265560 Feb 13 '14 at 05:11
  • TableLayout table=new TableLayout(this); TableRow tableRow=new TableRow(this); Button button=new Button(this); TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); Button.setLayoutParams(trParams); tableRow.AddView(button); table.addView(tableRow); – user3265560 Feb 13 '14 at 05:19

1 Answers1

0

You have called setLayoutParams on Button not button. Names are case sensitive. Button is the class while button is your object. Try:

TableLayout table=new TableLayout(this);
TableRow tableRow=new TableRow(this);
Button button=new Button(this);
TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 
tableRow.AddView(button, trParams); 
table.addView(tableRow);

However this will not fix the main issue. According to the Reference docs all children of a TableRow are forced to MATCH_PARENT for their width.

You could try nesting your button inside another layout. For example put your button inside a horizontal LinearLayout, then put the LinearLayout into your TableRow.

Sound Conception
  • 5,263
  • 5
  • 30
  • 47
  • TableLayout table=new TableLayout(this); TableRow tableRow=new TableRow(this); Button button=new Button(this); TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); Button.setLayoutParams(trParams); tableRow.AddView(button); table.addView(tableRow); – user3265560 Feb 13 '14 at 05:16