Archives for 20 Jan,2010

You are browsing the site archives by date.

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(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));
}
}
Read More