Search code examples
androidtextviewupdating

Android updating textviews and alignment


I have 2 textviews side by side, one aligned right and one left to keep them together. The right side is simply a label for the type of measurement given in the left side. The problem is that when the left TextView is a large number, the two TextView's are side-by-side with no gap in the middle. But as soon as the left TextView becomes a small number, it becomes spaced apart from the right side.

For example, it might look like this during one cycle: 3000meters

And then like this on the next: 5 meters

I need it to keep them together. Is there something I can do to achieve this, xml side or code side when updating the view?

edit - okay I had to edit it in wordpad to get it to show correctly in here

<TableRow android:layout_width="fill_parent"
    android:background="@drawable/maindatabg"> 
            <TableLayout 
                android:layout_gravity="center_horizontal"
                android:stretchColumns="*">
            <TableRow>
               <TextView
                  android:id="@+id/spaceValue"
                  android:text="00.0"
                  android:textStyle="bold"
                  android:textColor="#FFFFFF"
                  android:layout_gravity="right"
                  android:textSize="20dip"/>
              <TextView
                  android:id="@+id/spaceTypeValue"
                  android:layout_height="fill_parent"
                  android:text="meters"
                  android:textStyle="bold"
                  android:textColor="#FFFFFF"
                  android:layout_gravity="left"
                  android:textSize="14dip"/>
          </TableRow>
      </TableLayout>                                                         
</TableRow>

Solution

  • Mention android:gravity="right" to android:id="@+id/spaceValue". A sample layout and tested works fine.

    <?xml version="1.0" encoding="utf-8"?>
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent" >
    <TableLayout android:layout_gravity="center_horizontal"
        android:stretchColumns="*">
        <TableRow>
            <TextView android:id="@+id/spaceValue" android:text="3000"
                android:textStyle="bold" android:textColor="#FFFFFF"
                android:layout_gravity="right"  android:gravity="right"
                android:textSize="20dip" />
            <TextView android:id="@+id/spaceTypeValue"
                android:layout_height="fill_parent" android:text="meters"
                android:textStyle="bold" android:textColor="#FFFFFF"
                android:layout_gravity="left" android:textSize="14dip" />
        </TableRow>
    </TableLayout>
        </TableRow>