separation between rows in table layout
Asked Answered
H

6

20

I am displaying one table layout, in that I want separation line between rows in the table.Also is it possible to have column wise separation in table layout.Please help me.

Following is my xml table layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="6dip"
    android:paddingTop="4dip">

    <TableLayout
        android:id="@+id/tablelayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingRight="2dip">

        <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Income"></TextView>

            <TextView
                android:layout_width="150px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="150dp"
                android:text="Expense"></TextView>
        </TableRow>

        <TableRow android:layout_marginTop="30px">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Household:"></TextView>

            <TextView
                android:id="@+id/text50"
                android:layout_width="150px"
                android:layout_height="wrap_content"
                android:text="Household:"></TextView>
        </TableRow>


        <TableRow android:layout_marginTop="40px">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2"
                android:text="Travel:"></TextView>

            <TextView
                android:id="@+id/text51"
                android:layout_width="150px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-250dp"
                android:text="Travel"></TextView>
        </TableRow>

        <TableRow android:layout_marginTop="40px">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2"
                android:text="Education:"></TextView>

            <TextView
                android:id="@+id/text52"
                android:layout_width="150px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-250dp"
                android:text="Education"></TextView>
        </TableRow>

    </TableLayout>
</LinearLayout>
Hyrax answered 10/7, 2012 at 10:31 Comment(2)
As u are adding 2 texts in table row,add one image(line)/view as divider between text having width 2dp.this will work as dividerWilterdink
you can also try this. #2108956 or #7118033Fregger
M
24

Check this. It will work.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:paddingBottom="6dip"
    android:paddingTop="4dip" >

    <TableLayout
        android:id="@+id/tablelayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingRight="2dip" >

        <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Income" >
            </TextView>

            <TextView
                android:layout_width="150px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="150dp"
                android:text="Expense" >
            </TextView>
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <View
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="1dip"
                android:layout_weight="1"
                android:background="#FF909090"
                android:padding="2dip" />
        </TableRow>

        <TableRow android:layout_marginTop="30px" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Household:" >
            </TextView>

            <TextView
                android:id="@+id/text50"
                android:layout_width="150px"
                android:layout_height="wrap_content"
                android:text="Household:" >
            </TextView>
        </TableRow>
       </TableLayout>

</LinearLayout>
Measles answered 10/7, 2012 at 11:4 Comment(3)
Can we do the same programmatically? cause i am creating programmatic based layout.Pastille
I think this is not the write way of doing it. if you have 20 table rows then you have to create 20 rows more for the divider..... I think there is tag named as "android:devider" in TableLayout.. but i dont know how to use it ... if some body knows please tell soon...Convocation
Don't forget to add android:layout_span="2" to the divider views.Indomitable
O
23

If you only want to add a divider between rows then TableRow division has a good solution.

<TableLayout    
     android:divider="?android:attr/dividerHorizontal"
     android:showDividers="middle"
    ...
Overspend answered 21/6, 2016 at 1:43 Comment(0)
K
10

The TableLayout extends LinearLayout so you can add dividers in xml. I assume this would work back then:

    <TableLayout
        android:id="@+id/basket_summary_table"
        android:showDividers="middle"
        android:divider="@drawable/divider_list"
        android:padding="@dimen/element_large_spacing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">
    </TableLayout>

My divider looks like the following:

divider_list.xml

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:height="1dp" />
    <solid android:color="@color/list_divider" />
</shape>
Kreisler answered 4/5, 2016 at 9:20 Comment(0)
A
4
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);

from here.

Allotropy answered 10/7, 2012 at 10:36 Comment(3)
Thanks Vinay Wadhwa..Can you please tell me where i have to add this??Hyrax
Add a view in your xml between two textview rather than doing progrmaticallyWilterdink
you can add this code in the activity that uses the xml you mentioned in your question.Allotropy
T
2

I think you have to play with the background of the TableLayout and the margin of your rows...

Ty answered 10/7, 2012 at 10:40 Comment(1)
This is how I did it, too, ant it is the best solution because it avoids view bloat in the layout.Dumdum
H
0

I have made some changes in your code and think this will help you...

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">    
<TableLayout 
   android:id="@+id/tablelayout"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:paddingRight="2dip">

    <TableRow  >
 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Income">
     </TextView>            
        <TextView             
            android:layout_width="150px" android:layout_height="wrap_content" android:text="Expense" android:layout_marginLeft="150dp">
     </TextView>   
    </TableRow>
<View   android:id="@+id/firstDivider"
        android:layout_height="2dp"
        android:layout_width="fill_parent"
        android:background="#FFFFFF" />


    <TableRow  android:layout_marginTop="30px">
 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Household:" >
     </TextView>            
        <TextView             
            android:id="@+id/text50" android:layout_width="150px" android:layout_height="wrap_content" android:text="Household:">
     </TextView>   
    </TableRow>
<View   android:id="@+id/firstDivider"
        android:layout_height="2dp"
        android:layout_width="fill_parent"
        android:background="#FFFFFF" />



 <TableRow  android:layout_marginTop="40px">
 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Travel:" android:layout_span="2">
     </TextView>     
      <TextView
           android:id="@+id/text51"
           android:layout_width="150px" android:layout_height="wrap_content" android:text="Travel" android:layout_marginLeft="-250dp">
     </TextView>  


 </TableRow>


 <View   android:id="@+id/firstDivider"
        android:layout_height="2dp"
        android:layout_width="fill_parent"
        android:background="#FFFFFF" />

     <TableRow  android:layout_marginTop="40px">
 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_span="2">
     </TextView>     
      <TextView
           android:id="@+id/text52"
           android:layout_width="150px" android:layout_height="wrap_content" android:text="Education" android:layout_marginLeft="-250dp">
     </TextView> 

 </TableRow>

<View   android:id="@+id/firstDivider"
        android:layout_height="2dp"
        android:layout_width="fill_parent"
        android:background="#FFFFFF" />


</TableLayout>
</LinearLayout>

Thanks....

Hellbox answered 10/7, 2012 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.