Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

row

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?

like image 414
Muhammad Irfan Avatar asked Dec 15 '22 17:12

Muhammad Irfan


1 Answers

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>
like image 140
Dharmendra Avatar answered Jan 13 '23 14:01

Dharmendra