Saurabh Nanda
Saurabh Nanda

Reputation: 6793

TableLayout (TableRow?) not sizing child Views as expected when adding them dynamically

Context: I have a TableLayout (created using XML), which has one TableRow, which has one TextView. The code:

 <ScrollView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:fillViewport="true"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
  <TableLayout
     android:id="@+id/mytable"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:stretchColumns="1"
     >
    <TableRow>
      <TextView
     android:id="@+id/add_alarm"
     android:layout_weight="1"
     android:layout_width="0dp"
     android:layout_height="fill_parent"
     android:gravity="center"
     android:text="New\nItem"
     android:textSize="30sp"
     />
    </TableRow>
  </TableLayout>
</ScrollView>

In my Activity's onCreate() method, I am trying to add another View to the TableRow dynamically. Here is the code:

    public void onCreate(Bundle savedInstanceState)    {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(this);
        View mainLayout = inflater.inflate(R.layout.main, null);
        TableLayout tl = (TableLayout) mainLayout.findViewById(R.id.mytable);

        TableRow tr = (TableRow) tl.getChildAt(0);
        Log.d(TAG, "tr class = " + tr.getClass().getName() + " | width = " + tr.getWidth() + "\n");
        final RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.alarm_widget, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
        tr.addView(rl, lp); 
        tl.invalidate();
        setContentView(mainLayout);
}

Question: This code is not having the intended effect of displaying both the Views (the one in the XML layout already & the other added dynamically) in a columns of equal width.

  1. With the code given above, the dynamically added View has a width of '0' and is therefore invisible.
  2. If I change the code to tr.addView(rl) (i.e. without reference to LayoutParams), the dynamically added view is visible, but the columns are not equal in width.

How can I achieve this?

Edit: I changed the code based on the comments to the following. It still doesn't work as expected:

TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                               TableLayout.LayoutParams.WRAP_CONTENT, 1f);
tr.addView(rl, lp); 

Upvotes: 1

Views: 4546

Answers (3)

alperk01
alperk01

Reputation: 343

Set height like this to the layout containing the ScrollView. It solved my own problem where tablelayout does not show last lines.

android:layout_height="0dp"

Upvotes: 0

josephus
josephus

Reputation: 8304

I think it's not a good idea to dynamically add an item to a TableRow - it defeats the purpose of using a table. Imagine if you add an item to the first row of the table, but not on the second, meaning the first row has more elements. It wouldn't look much like a table.

But if you insist,

From developer guide:

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

You may need to start looking on the layout_weight of each element. Try adding layout_weight=1 on the row's static elements, and then setting your dynamic RelativeLayout's weight to 1 before adding it to the row.

rl.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));

The last parameter is weight.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234837

The problem is this behavior defined for TableRow:

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

Rather than add your text views directly to the TableRow, have the TableRow hold a horizontal LinearLayout and add the second view to that holder.

(Also, using LinearLayout.LayoutParams for something that's going into a TableRow is wrong. You should have been using TableRow.LayoutParams. But that wouldn't be the way to get equal-width TextViews. Use a LinearLayout holder.)

Upvotes: 3

Related Questions