Search code examples
androidalignmentandroid-tablelayout

Android TableLayout Alignment


I have Layout XML Like this. This is inside LinearLayout.

       <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="1"
            android:background="@android:color/background_dark">

                <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=" Hello world"
                android:textSize="20dp"
                android:layout_gravity="left"
                android:padding="2dp"
                android:background="#009900"
                android:textColor="@android:color/white"/>

                <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=" v1.0"
                android:textSize="20dp"
                android:padding="2dp"
                android:layout_gravity="right"
                android:background="#009900"
                android:textColor="@android:color/white"/>
        </TableRow>     
    </TableLayout>  

I want to display the row like this:

 [ Title                     v1.0 ]

But it displays like this:

 [ Title  v1.0                    ]

Solution

  • Try this.

     <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="1"
                android:background="@android:color/background_dark">
    
                    <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text=" RECSOLU Campus"
                    android:textSize="20dp"
                    android:layout_gravity="left"
                    android:padding="2dp"
                    android:layout_weight="1"
                    android:background="#009900"
                    android:textColor="@android:color/white"/>
    
                    <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="v1.0"
                    android:gravity="right"
                    android:layout_weight="1"
                    android:textSize="20dp"
                    android:padding="2dp"
                    android:layout_gravity="right"
                    android:background="#009900"
                    android:textColor="@android:color/white"/>
            </TableRow>     
        </TableLayout>