0

Okay, so I'm trying to insert TableRows into a TableLayout programmatically every time a button is tapped. Each of these rows contain a TextView set to some String. My problem is, that although the TableRow is inserted successfully, the text within the TextView that is itself inside the TableRow won't show up.

Here is what I'm doing:

    //prepare TextView to put inside a TableRow
    TextView text = new TextView(MyClass.this);
    text.setText(someString);
    text.setTextSize(15);
    text.setTextColor(Color.parseColor("#FF9900"));
    text.setLayoutParams(new LayoutParams
            (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


    //Prepare TableRow to be inserted
    TableRow row = new TableRow(MyClass.this);
    row.addView(text);
    row.setPadding(4,4,4,4);
    row.setGravity(Gravity.CENTER);
    row.setLayoutParams(new LayoutParams
            (LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

    //add row to the table
    someTableLayout.addView(row);

The result: A row is inserted, but I can't see the text (I know the row is inserted since I can see the dividers between each row). I'm guessing it has something to do with the LayoutParams? I'm probably overlooking something very stupid... but I have no idea...

Screenshot after I click Roll which should insert a row with the string/TextView: enter image description here

The "Start By Rolling" TextView is the first row which I put in through the XML file. It displays fine, but the rows inserted programmatically don't

u3l
  • 3,342
  • 4
  • 34
  • 51

2 Answers2

1

First this is you have a TextView with height match_parent and a tablerow with height wrap_content so make TextView height to wrap_content as well.

And second assign appropriate LayoutParams for ex:

TextView text = new TextView(MyClass.this);
text.setText(someString);
text.setTextSize(15);
text.setTextColor(Color.parseColor("#FF9900"));
text.setLayoutParams(new TableRow.LayoutParams
        (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


//Prepare TableRow to be inserted
TableRow row = new TableRow(MyClass.this);
row.addView(text);
row.setPadding(4,4,4,4);
row.setGravity(Gravity.CENTER);
row.setLayoutParams(new TableLayout.LayoutParams
        (LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

//add row to the table
someTableLayout.addView(row);
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • Thank you! This is perfect. Also, why do we have to use the appropriate LayoutParams? Wouldn't just using `LayoutParams` directly work anyways? – u3l Jan 11 '14 at 10:05
  • Every layout param is different it defines the properties with respect to the parent layout. You can allways use ViewGroup.LayoutParams but if you want some specific properties to apply like weigth in linearlayout you should have appropriate layoutparam – vipul mittal Jan 11 '14 at 10:21
0

Try this out:

text.setLayoutParams(new LayoutParams
            (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


text.setLayoutParams(new LayoutParams
            (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Nitesh Tiwari
  • 4,742
  • 3
  • 28
  • 46