SUPLMMM
SUPLMMM

Reputation: 13

Adding Rows to Table Layout Programmatically

Hi i would like to get a table layout to fill with the data of a XML when the activity first starts but im kind of a newbie so i really have no idea on how to do it if someone know how could i possibly do this

Upvotes: 1

Views: 10895

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

You can add table row dynamically as

for(int i=0;i<youxmlArray.size();i++){
    // get a reference for the TableLayout
    TableLayout table = (TableLayout)findViewById(R.id.TableLayout01);
    // create a new TableRow
    TableRow row = new TableRow(this);
    // create a new TextView for showing xml data
    TextView t = new TextView(this);
    // set the text to "text xx"
    t.setText( "YOUR XML DATA  HERE");
    // add the TextView  to the new TableRow
    row.addView(t);
    // add the TableRow to the TableLayout
    table.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
}

and Add a TableLayout in your xml layout as

<TableLayout 
android:id = "@+id/TableLayout01" 
android:layout_width = "fill_parent" 
android:layout_height = "wrap_content"   
android:stretchColumns = "0" >
   < TableRow android:id = "@+id/TableRow01" 
   android:layout_width = "wrap_content" 
   android:layout_height = "wrap_content" >
      < TextView android:id = "@+id/TextView01" 
        android:layout_width = "fill_parent" 
        android:layout_height = "wrap_content" 
        android:text = "textfield 1-1" >
        </ TextView >
   </ TableRow >
</ TableLayout > 

Upvotes: 3

Related Questions