Search code examples
androidspinnerright-align

Spinner alignment in TableView


I am trying to right align a spinner in a 2 column table in Android.

This is what I have tried so far:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/object_background">

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

            <Spinner
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:gravity="right"/>
        </TableRow>

Solution

  • 1) Use android:layout_width="0dp" and android:layout_weight="1" at your textView component

    2) Set android:layout_width="match_parent" to your tableRow.

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Test" />
    
            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>
    

    Output:

    enter image description here