How can I add separating lines between my TableRows that are created programmatically?
Asked Answered
C

3

18

I have a TableLayout that is created programmatically in an Android project. I keep adding TableRows as long as there are more rows fetched from the database. Now I want to add separating lines, like a border, between the TableRows.

In my other TableLayout that I created statically from XML I used a View as a separator, style with a style.xml.

I tried adding a View to the tablelayout like so:

View v=new View(this);
         v.setLayoutParams(new LayoutParams(
                 LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT));
         v.setBackgroundResource(R.drawable.rowseparator_shape);
             tr.addView(mTvDate);
             tr.addView(mTvResult);

             tl.addView(tr); 
             tl.addView(v);

But it only gets added once after all the collected TableRows. What would be a smart way of adding one View for each tr added? Or should I use something else alltogether?

Cleasta answered 23/2, 2011 at 14:21 Comment(0)
J
37
View v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
tr.addView(mTvDate);
tr.addView(mTvResult);

tl.addView(tr); 
tl.addView(v);

Here I'm creating a view that is one pixel high with a specific background color. This works for me.

Jaclynjaco answered 23/2, 2011 at 14:33 Comment(3)
TableRow.LayoutParams.FILL_PARENT is deprecated now.Manslaughter
i have multiple rows in my layout and for each row when i declare like this it is showing error.Bauer
Is there any method to do it in xml using the android:divider tagAckerman
P
15

Thanks to Madhusuthanan for this. I spent a while searching for how to do this to simply separate TextViews with a horizontal line. I was creating my view programmatically (without using a Table layout). Here is what I came up with based on the above answer:

View line = new View(this);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
line.setBackgroundColor(Color.rgb(51, 51, 51));
layout.addView(line);

Simple! Hope this helps someone else!

Polynices answered 15/2, 2012 at 15:5 Comment(1)
worked properly, according to my requirement... Thanks ChasdenModule
U
1

You can use Listview that will be easiler and better than doing this.

Uvea answered 23/2, 2011 at 14:28 Comment(1)
Yes, I have read about the benefits with a ListView over TableLayout. But I find TableLayout much easier to style, so that's why I chose it.Cleasta

© 2022 - 2024 — McMap. All rights reserved.