Aram
Aram

Reputation: 331

Align textviews to left and right

I have problem with textviews alignment. I want to align two textviews for the listview item to left and to right. In the Android Studio editor it's ok but when I'm deploying application to the device it's not right aligned. I don't know what is the problem. Below is my layout:

Found the problem. Issue actually was with the listview by mistake I set Listview's layout_width="wrap_content" but should be match_parent. As the mentioned layout is the list's item layout. Thanks everyone.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="@dimen/activity_horizontal_margin"
              android:orientation="vertical">

    <TextView
        android:id="@+id/tv_order_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/tv_order_category_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/tv_order_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="35sp"/>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_tin_ein"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_tin_ein"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_weight="1"
            android:text="Test"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="@string/order_address"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_email"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_email"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_phone"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_phone"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_mobile"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_mobile"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_fax"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_fax"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_created_date"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_created_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_start_working_date"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_start_working_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_end_date"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_end_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_cost"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_cost"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_vat"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_vat"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

</TableLayout>   

Upvotes: 0

Views: 366

Answers (2)

BorbagUskada
BorbagUskada

Reputation: 109

Try this :

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Text1"
                android:layout_weight="1"/>
            <TextView
                android:text="Text2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

Upvotes: 1

Long Phan
Long Phan

Reputation: 56

I think your code it's fine, and this one will improve for your code better. Just using gravity:right if you wanna put it on the right.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="asd"
            android:gravity="left"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_order_tin_ein"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="Test" />
    </LinearLayout>

Upvotes: 1

Related Questions