SkyWalker
SkyWalker

Reputation: 855

Android: Table Layout alignment

enter image description here

I want 'Duration' and 'Cost' Right aligned. I tried this code but it's not working.

<?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" >

<com.markupartist.android.widget.ActionBar
    android:id="@+id/actionBar"
    style="@style/ActionBar" />

<TableLayout 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
   <TableRow
       android:layout_width="fill_parent"
       android:layout_height="wrap_content">
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="5dip"
           android:text="Date" />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="5dip"
           android:text="Couse Name" />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="5dip"
           android:layout_gravity="right"
           android:text="Duration" />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="5dip"
           android:paddingRight="5dip"
           android:layout_gravity="right"
           android:text="Cost" />
   </TableRow>       
 </TableLayout>    
</LinearLayout>

layout_gravity="right" not working. I trird gravity="ritght" but that's also not working. I also want each column to center aligned.

Upvotes: 0

Views: 1765

Answers (4)

Rakhita Wickramatunge
Rakhita Wickramatunge

Reputation: 4503

Linear Layout is fine... Check this.. `

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Date" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Couse Name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:paddingLeft="5dip"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Duration" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Cost" />
    </TableRow>
</TableLayout>

`

Change each layout_weight as you wish.. Hope this helps.. Good luck...

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128428

For the same kind of layout, its better to use RelativeLayout.

Why to use RelativeLayout?

Just because we can set views by using below attributes:

  1. android:layout_toRightOf
  2. android:layout_toLeftOf
  3. android:layout_alignParentRight
  4. android:layout_alignParentLeft
  5. android:layout_below

and many more attributes RelativeLayout provides to set views.

<RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="5dip"
           android:text="Date"
           android:id="@+id/txtDate"/>

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="5dip"
           android:text="Couse Name"
           android:layout_toRightOf="@+id/txtDate" />

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="5dip"
           android:layout_gravity="right"
           android:text="Duration"
           android:layout_toLeftOf="@+id/txtCost" />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingLeft="5dip"
           android:paddingRight="5dip"
           android:layout_gravity="right"
           android:text="Cost"
           android:layout_alignParentRight="true"
           android:id="@+id/txtCost" />

</RelativeLayout>

Upvotes: 0

Sniper
Sniper

Reputation: 2452

give weight for Table row and all the text views...

    <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            android:text="Date" android:layout_weight="1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            android:text="Couse Name" android:layout_weight="1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:paddingLeft="5dip"
            android:text="Duration" android:layout_weight="1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:text="Cost" android:layout_weight="1"/>
    </TableRow>
</TableLayout>

Upvotes: 2

Goofy
Goofy

Reputation: 6128

Hey you can use Relative layout so you can place your text views any where you want...

below is a sample code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF" >

   <TextView 
       android:id="@+id/usage"
       android:layout_marginTop="220dip"
       android:layout_marginLeft="45dip"
       android:layout_marginRight="15dip"
       android:typeface="serif"            
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text=" "
       android:textColor="#030900"/>

</RelativeLayout>

Upvotes: 0

Related Questions