Last active
November 22, 2018 08:08
-
-
Save AB1209/8b51705e8d7d4dd801a3f5d1b6e5d97f to your computer and use it in GitHub Desktop.
Demonstrating creation of dynamic TableLayout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment