How to access views inside TableLayout

John Watson picture John Watson · Jul 16, 2012 · Viewed 12.7k times · Source

In TableLayout there are 9 Buttons in a 3x3 format. How to access the text on these buttons programatically using the id of TableLayout (not Button Id) ?

Answer

user370305 picture user370305 · Jul 16, 2012

Use something like,

TableLayout tblLayout = (TableLayout)findViewById(R.id.tableLayout);
TableRow row = (TableRow)tblLayout.getChildAt(0); // Here get row id depending on number of row
Button button = (Button)row.getChildAt(XXX); // get child index on particular row
String buttonText = button.getText().toString();

3x3 format: (Code for understanding actual may be different)

for(int i=0;i<3;i++)
{
 TableRow row = (TableRow)tblLayout.getChildAt(i);
  for(int j=0;j<3;j++){
    Button button = (Button)row.getChildAt(j); // get child index on particular row
    String buttonText = button.getText().toString();
    Log.i("Button index: "+(i+j), buttonText);
 }
}