Samuel909 Samuel909 - 4 months ago 9
Android Question

android textview not aligning side by side

I have 4 textviews as -

Departure Timinghello
hello Time
45 sec
9


As you see -

1) In
Departure hello
, 'h' does not start below 'D'. It starts after 1 space.

2) '9' is not in line with '45 sec'

How do I achieve this?

Answer

Use this kind of a layout (repeat the inner LinearLayout for each row):

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>

    <LinearLayout
    android:layout_width="match_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    >

    <TextLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:weight="1"
    android:gravity="left"
    android:text="Departure"
    />

    <TextLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:weight="1"
    android:gravity="left"
    android:text="TimingHello"
    />

    </LinearLayout>

</LinearLayout>

Do not forget the layout_width as 0dp or you will get garbage.

Also best for this kind of a layout would be to use:

TableLayout
Comments