Dynamically add rows to TableLayout - Problem


Last Updated:

  1. SalvoMaltese

    SalvoMaltese Member This Topic's Starter

    Joined:
    Aug 19, 2009
    Messages:
    8
    Likes Received:
    0
    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 (Text):
    1.  
    2. package com.ProgGrid;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.ViewGroup.LayoutParams;
    7. import android.widget.Button;
    8. import android.widget.TableLayout;
    9. import android.widget.TableRow;
    10.  
    11. public class ProgGrid extends Activity {
    12.     /** Called when the activity is first created. */
    13.     @Override
    14.     public void onCreate(Bundle savedInstanceState) {
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.main);
    17.  
    18.         /* Find Tablelayout defined in main.xml */
    19.         TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
    20.              /* Create a new row to be added. */
    21.              TableRow tr = new TableRow(this);
    22.              tr.setLayoutParams(new LayoutParams(
    23.                             LayoutParams.FILL_PARENT,
    24.                             LayoutParams.WRAP_CONTENT));
    25.                   /* Create a Button to be the row-content. */
    26.                   Button b = new Button(this);
    27.                   b.setText("Dynamic Button");
    28.                   b.setLayoutParams(new LayoutParams(
    29.                             LayoutParams.FILL_PARENT,
    30.                             LayoutParams.WRAP_CONTENT));
    31.                   /* Add Button to row. */
    32.                   tr.addView(b);
    33.         /* Add row to TableLayout. */
    34.               tl.addView(tr,new TableLayout.LayoutParams(
    35.                   LayoutParams.FILL_PARENT,
    36.                   LayoutParams.WRAP_CONTENT));
    37.         setContentView(tl);
    38.    }
    39. }
    while my main.xml is:

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:id="@+id/myTableLayout"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent">
    7.      <TableRow
    8.           android:layout_width="fill_parent"
    9.           android:layout_height="wrap_content">
    10.            
    11.           <Button android:text="Static Button" android:id="@+id/myButton"/>
    12.      </TableRow>
    13. </TableLayout>        
    14.  
    But when I launch my app, I can see only "Static Button", but not the dinamically added "Dynamic Button".

    Where's my error?
     

    Advertisement
  2. jksharma

    jksharma New Member

    Joined:
    Oct 15, 2009
    Messages:
    4
    Likes Received:
    0
    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
     
  3. jksharma

    jksharma New Member

    Joined:
    Oct 15, 2009
    Messages:
    4
    Likes Received:
    0
    And error is As I think that you can not use setLayoutParams with a button
     
  4. jorgensen

    jorgensen New Member

    Joined:
    May 10, 2011
    Messages:
    2
    Likes Received:
    1
    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.
     
    jeysterg likes this.

Share This Page

Loading...