Skip to content

Android: Eclipse and problems with dynamic tables (adding rows)

by mat on January 20th, 2010

When using TableLayout with LayoutParams Eclipse will automatically (and wrongly) add the following import:

import android.view.ViewGroup.LayoutParams;

However if you try to use this you will notice that nothing changes. You need to replace the import with the ollowingL

import android.widget.TableRow.LayoutParams;

Now if you rerun your code, you will see that your tables are updating. See below for an example of the code to generate a table

int sizey=10;
int sizex=10;
int btnwidth=24;
TableLayout tl = (TableLayout)findViewById(R.id.MyTableLayout); 

for (int y=0;y<sizey;y++){

// Rows
TableRow tr = new TableRow(this); 
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

//Cells
for (int x=0;x<sizex;x++){
// Create new cell
	// new button
	Button b = new Button(this); 
	b.setText(x+""+y); 
	b.setLayoutParams(new LayoutParams( 
		LayoutParams.FILL_PARENT, 
		LayoutParams.WRAP_CONTENT));
	b.setHeight(btnwidth);
	b.setBackgroundResource(R.drawable.btn_blue);
	b.setOnClickListener(new PegOnClickListener(y,x));
	// add button to row
	tr.addView(b); 
	}
// add row to layout
tl.addView(tr,new TableLayout.LayoutParams( 
LayoutParams.FILL_PARENT, 
LayoutParams.WRAP_CONTENT));
}
}
7 Comments
  1. nova permalink

    This is a general problem with eclipse and auto imports (control+shift+o). I’ve noticed this with automatically importing java.sql.Date rather than java.util.Date also. Just make sure that the imports that eclipse things you need are the imports that you think you need :)

  2. zOro permalink

    Hey
    Its look like a greate toturail of dymamic table,
    can you display the resource file ?

    thanks for sharing (:

  3. Steve permalink

    Thanks! This was a great solution. I’ve been hunting down the issue for 2 days!

  4. Ready4Fajir permalink

    Hey there,

    THANKS – this issue was almost killing me LOL. I spent one hour trying why that freaking table won’t show xD – you saved me days I guess – THANKS !

  5. Yes – this is spot on. Scratching my head on this for almost an hour. All Views you dynamically add in the Table need to use TableRow or TableLayout LayoutParams, not View.LayoutParams. Imports messed this up. Thanks!

  6. MattTapp permalink

    Thanks mate!
    I was struggling with it for an hour!

  7. uno permalink

    You may use
    tr.setLayoutParams(new TableView.LayoutParams(….

Leave a Reply

Note: I am currently writing my thesis so probably wont have time to reply to your comment
Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS