Search code examples
androidtextviewalignment

How to align all TextView in android?


I have a TextView. I would like to do all TextView (No.123... & Sea View...) align left. I know android:gravity="left" android:layout_gravity="left"but it is not suit for my problem.

Here is my xml code;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Address " />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="No.123, Blah Street, Blah Township, Blah Country"  />

        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Type "/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sea View, Graden View, Local" />

        </LinearLayout>
</LinearLayout>

How to do it with align left text view? Like below picture.

enter image description here

Output Result;

enter image description here


Solution

  • Use Relative Layout instead to achieve what you want,

     <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Address  " />
    
            <TextView android:id="@+id/type"
                android:layout_below="@+id/address"
                android:layout_alignRight="@+id/address"
                android:layout_width="match_parent"
                android:gravity="left"
                android:layout_height="wrap_content"
                android:text="Type  "/>
    
            <TextView
                android:layout_toRightOf="@+id/address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="No.123, Blah Street, Blah Township, Blah Country"  />
    
            <TextView
                android:layout_alignBottom="@+id/type"
                android:layout_toRightOf="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sea View, Graden View, Local" />
        </RelativeLayout>
    

    enter image description here