• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Dynamically add rows to TableLayout - Problem

Hi All,

I'm trying to use [TinyTut] - Dynamically add rows to TableLayout :: anddev.org - Android Development Community | Android Tutorials .

Now my java code is this:
Code:
package com.ProgGrid;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

public class ProgGrid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /* Find Tablelayout defined in main.xml */ 
        TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout); 
             /* Create a new row to be added. */ 
             TableRow tr = new TableRow(this); 
             tr.setLayoutParams(new LayoutParams( 
                            LayoutParams.FILL_PARENT, 
                            LayoutParams.WRAP_CONTENT)); 
                  /* Create a Button to be the row-content. */ 
                  Button b = new Button(this); 
                  b.setText("Dynamic Button"); 
                  b.setLayoutParams(new LayoutParams( 
                            LayoutParams.FILL_PARENT, 
                            LayoutParams.WRAP_CONTENT)); 
                  /* Add Button to row. */ 
                  tr.addView(b); 
        /* Add row to TableLayout. */ 
              tl.addView(tr,new TableLayout.LayoutParams( 
                  LayoutParams.FILL_PARENT, 
                  LayoutParams.WRAP_CONTENT));
        setContentView(tl);
   }
}

while my main.xml is:

Code:
<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTableLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     <TableRow 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content"> 
           
          <Button android:text="Static Button" android:id="@+id/myButton"/> 
     </TableRow> 
</TableLayout>

But when I launch my app, I can see only "Static Button", but not the dinamically added "Dynamic Button".

Where's my error?
 
hi,
I have done this before:
Please try this code.

setContentView(R.layout.contactscreen);
TableLayout table = (TableLayout)findViewById(R.id.contactLayout);
TableRow row=null;
TextView label = null;
ImageButton button = null;
ButtonClickListener blc=new ButtonClickListener();
int size=contacts.size();
for (int i = 0; i < size; i++)
{
row = new TableRow(this);
row.setId(100+i);
row.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

//row.setBackgroundColor(0xff222222);
row.setOnClickListener(blc);
button = new ImageButton(this);
//button.setBackgroundColor(0xff222222);
if(contactStatus.elementAt(i).toString().equals("online"))
button.setImageResource(R.drawable.greendragonsmall);
else
button.setImageResource(R.drawable.skydragonsmall);
label = new TextView(this);
label.setText(contacts.elementAt(i).toString());
label.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
label.setGravity(Gravity.CENTER_VERTICAL);

if(i%2==0)
{
row.setBackgroundColor(0xff222222);
button.setBackgroundColor(0xff222222);
}
else
button.setBackgroundColor(0xff000000);
row.addView(button);
row.addView(label);

table.addView(row);


}

check if it works
 
Had a similar problem like this one, and managed to fix it by replacing the line
- b.setLayoutParams(new LayoutParams(
with
- b.setLayoutParams(new TableRow.LayoutParams(

Havn't tested it with this example.
 
Back
Top Bottom