0

I have a Table Layout defined within LinearLayout as follows:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#E6E6E6"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/fbTableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@android:color/darker_gray"
        android:stretchColumns="*" >

    </TableLayout>
</LinearLayout>

I am adding dynamic rows to the TableLayout as follows:

fbTableLayout = (TableLayout) findViewById(R.id.fbTableLayout);
    for (int i = 0; i < 4; i++) {
        fbTableRow = new TableRow(this);
        fbTableRow.setBackgroundColor(Color.WHITE);
        LayoutParams layoutParams = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        int leftMargin=10;
        int topMargin=2;
        int rightMargin=10;
        int bottomMargin=2;

        layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

        fbTableRow.setLayoutParams(layoutParams);

        ImageView iv = new ImageView(this);
        iv.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.ic_launcher));
        iv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT,
                0.25f));

        TextView tv = new TextView(this);
        tv.setText("Album "+ i);
        tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT,
                0.75f));

        fbTableRow.addView(iv);
        fbTableRow.addView(tv);
        fbTableLayout.addView(fbTableRow, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }

But I am not able to generate spaces between the rows generated. The layout is as shown in the figure attached.

enter image description here

I have gone through a number of solutions given in stackoverflow to resolve this issue but none of them are working for me. Not sure what I am missing. Any help would be greatly appreciated. Thanks in advance.

namrathu1
  • 347
  • 1
  • 7
  • 24

3 Answers3

1

Add another table row (in the loop) with a blank image. Specify the size of the image to create the size needed for your space.

llBuckshotll
  • 119
  • 1
  • 9
  • I tried this and it does work and gives the desired result. But I guess one should be able to do it by adjusting the margins. So I am going to try and see if I can get it to work by doing that. (If someone could respond to this post if they had faced a similar issue) Thanks a lot for your response.Appreciate it a lot. – namrathu1 Jan 02 '13 at 21:33
0

Have you tried this when you're adding the table row?

fbTableLayout.addView(fbTableRow, layoutParams);

If not, you can try setting the margins on the individual views within the table row, but I'm pretty sure the above should apply the layout params when the row is being added to the table layout.

Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
  • I tried both these approaches as you suggested but none of the worked. :( Is there anything else that I could try? – namrathu1 Jan 02 '13 at 21:29
  • That's very strange; I know I've changed margins programatically myself (i.e., http://stackoverflow.com/a/3002321/832776) for other views. Looks like someone else had success with `TableRow`s though: http://stackoverflow.com/questions/4577644/programmatically-set-margin-for-tablerow – Oleg Vaskevich Jan 02 '13 at 21:39
  • Yes I have used the exact same example [http://stackoverflow.com/questions/4577644/programmatically-set-margin-for-tablerow] but that's not working for me. The only addition is my ` ` and the parent LinearLayout (as shown in my original code) . Do you feel something is wrong there? – namrathu1 Jan 02 '13 at 22:02
  • I don't know; it should work; I'd try fiddling around with a few things: 1) Remove all the XML properties (i.e. `stretchColumns`), 2) Try changing the target SDK, 3) Try running on a different device or with an emulator. Also just confirm and make sure you're using the right IDs everywhere; I can't tell you how often that's happened to me when I've been updating a different view than expected. – Oleg Vaskevich Jan 02 '13 at 22:14
  • sure I will check all those. Thanks a lot for your response. – namrathu1 Jan 02 '13 at 23:15
0

The docs for the setMargins method include this note:

A call to requestLayout() needs to be done so that the new margins are taken into account. Left and right margins may be overriden by requestLayout() depending on layout direction.

...so one thing to try would be to call fbTableLayout.requestLayout() after you've added all the rows. This probably won't make a difference, since the view should be getting invalidated in the code you've already posted, but it wouldn't hurt to try.

If that doesn't work, you could use another viewgroup (e.g. a FrameLayout) within each table row to contain your ImageView and TextView and set your padding there.

Lorne Laliberte
  • 6,261
  • 2
  • 35
  • 36