Question

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>
Was it helpful?

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

OTHER TIPS

I'm not sure if this works for a table but you could try using weights with gravity left/right (although this displays a warning about nested weights being bad for performance) however this is what I have used for the time being using a horizontal linear layout :

<TextView
            android:id="@+id/TextViewID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="0.4"
            android:text="@string/TextView" />

        <Spinner
            android:id="@+id/SpinnerID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="0.6" />

The above aligns my Spinner to the right of my Text View

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top