1

In my application I am displaying a table layout, in which I aligned 4 textviews in a row.

My table layout is: (I don't know how much good design is this)

<TableLayout 
    android:id="@+id/tablelayout"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:paddingRight="2dip"
    android:stretchColumns="4">
    <TableRow >
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Income" />
        <TextView
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Expense" 
            android:layout_marginLeft="-150dp" />
    </TableRow>    
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <View
            android:id="@+id/line1"
            android:layout_width="wrap_content"
            android:layout_height="1dip"
            android:layout_weight="1"
            android:background="#FF909090"
            android:padding="2dip" />
    </TableRow>
    <TableRow 
        android:id="@+id/tablerowhouse"   
        android:layout_marginTop="30px">
        <TextView  
            android:id="@+id/text40" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Household:" />            
        <TextView             
            android:id="@+id/houseinctot" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Household:" 
            android:layout_marginLeft="-250dp" />   
        <TextView 
            android:id="@+id/text41" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Household:" 
            android:layout_marginLeft="-150dp" />          
        <TextView
            android:id="@+id/houseexptot" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Household:" 
            android:layout_marginLeft="-70dp" 
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <View
            android:id="@+id/line1"
            android:layout_width="wrap_content"
            android:layout_height="1dip"
            android:layout_weight="1"
            android:background="#FF909090"
            android:padding="2dip" />
    </TableRow>
    <TableRow 
        android:layout_marginTop="30px">
        <TextView
            android:id="@+id/text42" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Travel:" 
            android:layout_span="3" />    
        <TextView
            android:id="@+id/trlinctot"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Travel" 
            android:layout_marginLeft="-250dp" 
            android:layout_alignParentRight="true" /> 
        <TextView  android:id="@+id/text43" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Travel:" 
            android:layout_marginLeft="-150dp" />           
        <TextView             
            android:id="@+id/trlexptot" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Travel:" 
            android:layout_marginLeft="-70dp"  
            android:gravity="right" />    
    </TableRow>
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <View
            android:id="@+id/line1"
            android:layout_width="wrap_content"
            android:layout_height="1dip"
            android:layout_weight="1"
            android:background="#FF909090"
            android:padding="2dip" />
    </TableRow>


     <TableRow  android:layout_marginTop="30px">
 <TextView  android:id="@+id/text44" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_span="4">
     </TextView>     
      <TextView
           android:id="@+id/eduinctot"
           android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education" android:layout_marginLeft="-250dp" android:layout_alignParentRight="true">
     </TextView>  

           <TextView  android:id="@+id/text45" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_marginLeft="-150dp" >
     </TextView>            
        <TextView             
            android:id="@+id/eduexptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_marginLeft="-70dp"  android:gravity="right"
            >
     </TextView>    
 </TableRow>

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

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

        <TableRow  android:layout_marginTop="30px">
 <TextView  android:id="@+id/text46" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group4:" android:layout_span="4">
     </TextView>     
      <TextView
           android:id="@+id/grp4inctot"
           android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group4" android:layout_marginLeft="-250dp">
     </TextView>  

           <TextView  android:id="@+id/text47" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group4:" android:layout_marginLeft="-150dp" >
     </TextView>            
        <TextView             
            android:id="@+id/grp4exptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group4:" android:layout_marginLeft="-70dp"  android:gravity="right"
            >
     </TextView>    
 </TableRow> 

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

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

             <TableRow  android:layout_marginTop="30px">
 <TextView  android:id="@+id/text48" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group5:" android:layout_span="4">
     </TextView>     
      <TextView
           android:id="@+id/grp5inctot"
           android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group5" android:layout_marginLeft="-250dp">
     </TextView>  

           <TextView  android:id="@+id/text49" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group5:" android:layout_marginLeft="-150dp" >
     </TextView>            
        <TextView             
            android:id="@+id/grp5exptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group5:" android:layout_marginLeft="-70dp"  android:gravity="right"
            >
     </TextView>    
 </TableRow> 

Now what i want is:

textview1:value(textview2)     textview3:value(textview4)
textview5:value(textview6)     textview7:value(textview8) .

I should right allign textview2 and textview6 (with in half of the screen) and should right align textview6 and textview8

 What i have:                         What i want:
    12345   4565                         12345    4565
    12        56                            12      56
    56        44                            56      44  
    6          6                             6       6
Cœur
  • 37,241
  • 25
  • 195
  • 267
prakash .k
  • 635
  • 2
  • 12
  • 24

3 Answers3

0

You write android:gravity="right" property in all Textview.

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • No hardik joshi.If i use the above property means textview2 will overlap with textview4 and textview6 overlap with textview8 – prakash .k Jul 13 '12 at 06:44
0

You should use a relative layout where you have ALIGN_PARENT_RIGHT option.

Something like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingTop="8dip"
    android:paddingBottom="8dip"
>

<TextView  
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="turn on" 
/>

<CheckBox 
    android:id="@+id/chekcbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    /> 
</RelativeLayout>

android:layout_alignParentRight="true" will place your textview to extreme right. Just try once it worked for me.

Abhilasha
  • 929
  • 1
  • 17
  • 37
  • You can place textview2 to right of textview1..and also using `android:layout_alignParentRight="true"` will place your textview to extreme right. Each row of `TableLayout` can be a `RelativeLayout`. Or You can have `ListView` with each row as `RelativeLayout`.See my edit for complete xml code – Abhilasha Jul 13 '12 at 07:01
  • No offence. But did you try putting that line in your code? I guess that is far better than keep guessing what it will result in. It will surely save lot of your time. Just paste the code and see how it looks in graphical layout...Life made easy for you ! :) Or may be past your complete xml and we can suggest better things. – Abhilasha Jul 13 '12 at 07:14
  • Abhilasha..I tried what i said is coming...If only 2 textview in a row means v make it 1 as left and another as right.But i have 4 textview how can v make it as align left and right?? – prakash .k Jul 13 '12 at 07:26
  • try this...but this will only make your text justified to center... `android:gravity="center_horizontal"` Will be back with solution after lunch! I know relativeLayout works for this if tweaked in proper way...Meanwhile check this link for more pointers http://stackoverflow.com/q/1292575/840520 – Abhilasha Jul 13 '12 at 08:01
0

Try this layout.This works.

<?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="fill_parent"
    android:orientation="vertical" >

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

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="123456" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="1234" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_amount1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="123456" />
        </RelativeLayout>
    </LinearLayout>

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

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_label1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="16" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_amount3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="1" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/summary_amount4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="1" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>
Abhilasha
  • 929
  • 1
  • 17
  • 37