Saturday, April 5, 2014

Using TableLayout and dynamically populating views in Android

Useful links are follow: http://forum.codecall.net/topic/70354-tablelayout-programatically/

Sample Code:

public void drawScreen(ArrayList<Product> products) {
tl.removeAllViews();
sv.removeAllViews();

TableRow.LayoutParams params = new TableRow.LayoutParams();
params.gravity = Gravity.CENTER_VERTICAL;

for(int i=0;i<products.size();i++) {
TableRow tr = new TableRow(this);
//View v = (RelativeLayout)findViewById(R.layout.product_row);
TextView tv = new TextView(this);
ImageView iv = new ImageView(this);
CheckBox cb = new CheckBox(this);
cb.setOnClickListener(this);
checkboxes.add(cb);
iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
cb.setLayoutParams(params);
//cb.setOnCheckedChangeListener(this);
tv.setText(products.get(i).getAsin());
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setWidth(200);
tr.addView(iv);
tr.addView(tv);
tr.addView(cb);
tr.setGravity(Gravity.CENTER);
tr.setBackground(getResources().getDrawable(R.drawable.rectangle));
tl.addView(tr);
}
TableRow tr = new TableRow(this);
tr.setGravity(Gravity.CENTER);
compareButton = new Button(this);
compareButton.setText("Compare");
compareButton.setOnClickListener(this);
tr.addView(compareButton);
tl.addView(tr);
sv.addView(tl);
setContentView(sv);
}

No comments:

Post a Comment