Muhammad Irfan
Muhammad Irfan

Reputation: 1457

How to vertically align a textview field using table row in Android

I have this output

http://www.cubixshade.com/images/align_row.jpg

I want first field that is date field to be align vertically center? and what should i use use to add some space from left or right in other field also?

Upvotes: 4

Views: 14199

Answers (2)

Dharmendra
Dharmendra

Reputation: 33996

You can set android:gravity="center_vertical" property to TableRow to align its child vertically center.

Also you can use android:layout_marginLeft property or android:layout_marginRiight property to put some margins from left and right similarly you can put margin to top and bottom using android:layout_marginTop or android:layout_marginBottom.

If you want to stretched column then you can use the android:layout_weight property to define the weight of the column.

For reference you can see below example with 2 columns

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1" >

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

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Male"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etNoOfMale"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:inputType="number"/>
    </TableRow>

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Female"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etNoOfFemale"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:inputType="number"/>
    </TableRow>

</TableLayout>

Upvotes: 14

Rick
Rick

Reputation: 49

U can use tags likes below in your xml file page to align proper, If can give the details about the pblm you are facing I can give more explanation.

android:layout_marginLeft="25dp"
android:layout_below="@+id/imageView1"

the first code gives a margin of 25dp from the left and the second code just align the filed to right under the imageview1.

And there is one another method Just right click on the text-view or anything you added in the design page the select the Other properties->layout parameters->then select the item you need[it contains all the align properties.

Hope this one help you. Thank you

Upvotes: -1

Related Questions