now the code to adding something to a TableLayout is quite simple.
//set the content view from an XML file
setContentView(R.layout.main);
//first get the TableLayOut from the xml file that you previous loaded
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
//set the columns to resize so that the row gets the full length of the screen
table.setColumnStretchable(0, true);
table.setColumnCollapsed(1, true);
//useful for getting a resource by name
int resID = getResources().getIdentifier("filename", "drawable", "com.yourcompany.yourApp");
//now create a view you want to add to the tableLayout
//in this case a ImageView
ImageView img = new ImageView(activity);
img.setImageResource(ResID);
//set the height to 100 and width to the width of the parent
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 100));
//finally add to the tableLayout view
table.addView(img);
its quite simple, but now replace adding the ImageView to the Table with adding a TableRow to the Table which may have any number of views within. A TableRow is simply a LinearLayout.
//set the content view from an XML file
setContentView(R.layout.main);
//first get the TableLayOut from the xml file that you previous loaded
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
//set the columns to resize so that the row gets the full length of the screen
table.setColumnStretchable(0, true);
table.setColumnCollapsed(1, true);
//useful for getting a resource by name
int resID = getResources().getIdentifier("filename", "drawable", "com.yourcompany.yourApp");
TableRow row = new TableRow(this);
//the tablerow is suppose to use this layout params always according to sdk guidelines
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
//now create a view you want to add to the tablerow
//in this case a ImageView
ImageView img = new ImageView(this);
img.setImageResource(ResID);
//set the height to 100 and width to the width of the parent
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 100));
//add the ImageView to the row
row.addView(img);
//finally add to the tableLayout view
table.addView(row);
now if you have tried the above could nothing should come up, that's because certain views have their own LayoutParams class.
so what you need to do to make the above code work is change the following line.
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
with
row.setLayoutParams( new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
the basic thing is make sure to check your using the right LayoutParams with a given class, because if your not the platform won't know what to do with that layout.
Anyway though it was worthing mentioning cause it took me a while to figure it out.
good post. thanks
ReplyDeleteI hit into this and you saved me a lot of time. Thanks a lot!! I wish if you specified the wrong LayoutParams, it would at least throw a warning or something.
ReplyDeleteCan this code be modified to dynamically have more than one row? If yes, how do we do that in a for loop?
ReplyDelete