How to use dynamic TableLayout instead of using ListView in Android.

Using ListView for showing data in vertical rows format is always good option but things become difficult when you want to use any widgets in rows of ListView. Although it can be done by using CustomAdapter but when you want to perform action on those widgets (like checking CheckBox or etc.) things become terrifying. For implementing this you need to maintain state of each widget and that part is the difficult one.

For an experiment there is one more way by creating dynamic TableLayout. Although it is complex to implement but once you become familiar with it, it becomes simple. Here in this example I am creating a dynamic TableLayout whose each row contains a TextView and a Button.Button’s behaviour is made like CheckBox.So on clicking button you can make it check or uncheck.
The advantage of this over using dynamic ListView is you don’t need to worry about saving state of each widget. You can make it easy as any other widget which is static rather than created dynamically.

The XML code is simple.

And in the Java Code .
I have used three special variable

  1. final int CHECK_BUTTON_ID; :– This variable is used for creating ids of dynamically created button (or you can say CheckBox);
  2. int ids_check[]; :– This integer array is array of ids of each Button which is dynamically created in TableRow.
  3. boolean bool_check[]; :– This boolean array is used for keeping knowledge of each Button which is checked or unchecked.

And a very important method createTableRows() :– This method creates the TableRows and all these rows are added to TableLayout.
In this method I have used TableRow,TextView & Button variables whose properties are set by there respective methods.You can customize these by way you want.

*The most important thing in using this way of creating dynamic TableLayout import this android.widget.TableRow.LayoutParams
instead of android.view.ViewGroup.LayoutParams

This is a simple example of dynamic TableLayout with only two widgets you can use as many as you want. It seems to be quit complex but once you done with it you can use with very ease.


public class DynamicTable extends Activity {
private TableLayout table;
private ArrayList list_name;
private int color_blue = -16776961;
private int color_gray = -7829368;
private int color_black = -16777216;
private int color_white = -1;
private final int CHECK_BUTTON_ID = 982301;
private int ids_check[];
private boolean bool_check[];
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
table = (TableLayout) findViewById(R.id.tableLayout1);
list_name = new ArrayList();
list_name.add("Close");
list_name.add("Cristiano");
list_name.add("David");
list_name.add("Fernando");
list_name.add("Messi");
list_name.add("Kaka'");
list_name.add("Wayne");
bool_check = new boolean[list_name.size()];
ids_check = new int[list_name.size()];
createTableRows();
}
public void createTableRows() {
for (int i = 0; i < list_name.size(); i++) {
TableRow table_row = new TableRow(this);
TextView tv_name = new TextView(this);
Button btn_check = new Button(this);
ImageView img_line = new ImageView(this);
table_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
table_row.setBackgroundColor(color_white);
table_row.setGravity(Gravity.CENTER_HORIZONTAL);
tv_name.setText(list_name.get(i));
tv_name.setTextColor(color_blue);
tv_name.setTextSize(16);
tv_name.setTypeface(Typeface.DEFAULT_BOLD);
tv_name.setWidth(150);
btn_check.setLayoutParams(new LayoutParams(30, 30));
btn_check.setBackgroundResource(R.drawable.small_checkbox_unchecked);
img_line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2));
img_line.setBackgroundResource(R.drawable.separater_line);
table_row.addView(tv_name);
table_row.addView(btn_check);
table.addView(table_row);
table.addView(img_line);
int id = i + CHECK_BUTTON_ID;
btn_check.setId(id);
ids_check[i] = id;
btn_check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j < ids_check.length; j++) {
Button btn_check_1 = (Button) findViewById(ids_check[j]);
if (v.getId() == ids_check[j])
if (bool_check[j]) {
btn_check_1.setBackgroundResource(R.drawable.small_checkbox_unchecked);
bool_check[j] = false;
} else {
btn_check_1.setBackgroundResource(R.drawable.small_checkbox_checked);
bool_check[j] = true;
}
}
}
});
}
}
}

I hope you like it and want to use or try it.

Thanks

😍 Happy Coding 😍