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

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

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

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

for (int x=0;x<sizex;x++){
// Create new cell
	// new button
	Button b = new Button(this); 
	b.setLayoutParams(new LayoutParams( 
	b.setOnClickListener(new PegOnClickListener(y,x));
	// add button to row
// add row to layout
tl.addView(tr,new TableLayout.LayoutParams( 


  1. nova

    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

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

    thanks for sharing (:

  3. Steve

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

  4. Ready4Fajir

    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. MotherRobot

    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

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

  7. uno

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

Leave a Comment

Your email address will not be published. Required fields are marked *