0

i am using tablelayout with 3 columns lable, * and spinner, which all of that has wrap_content and it works fine when the spinner text is small. if any one of the spinner has very long text then all the spinner extents till the end and also it is not fitting to the screen.

is there any way to make each spinner to fit to its text size like wrap_content inside tablelayout. xml for table layout sample.

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:stretchColumns="2" >
      <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_column="0"
                android:gravity="right"
                android:paddingLeft="3dip"
                android:text="Sold To"
                android:textColor="#000033"
                android:textSize="17sp" />

            <TextView
                android:layout_column="1"
                android:gravity="left"
                android:text="*"
                android:textColor="#FF0000"
                android:textSize="17sp"
                android:width="20dip" />

            <Spinner
                android:id="@+id/sold_to_dd"
                android:layout_width="wrap_content"
                android:layout_column="2"
                 />
        </TableRow>


    </TableLayout>

i hope many would have come across the same problem. Thanks in advance

saran
  • 461
  • 1
  • 6
  • 20

2 Answers2

0

You can either set maxWidth attribute for each spinner or you can check the length of the string ie to be placed in spinner in case if its length > x (say 10) then use substring fn to take only (x-3) characters from the string and suffix it with " ... " and place that string in spinner. For eg : StringABCDEFGHIJKLM can become StringAB....

user2041902
  • 593
  • 1
  • 6
  • 21
  • thanks can u post the code, that where to check that condition – saran Feb 20 '13 at 06:13
  • you have to perform the substring operation on each string that you are adding into the spinner. If you are populating the spinner with an `ArrayList` do the substring operation on each string before adding it into the spinner – user2041902 Feb 20 '13 at 06:38
0

This is because you are stretching only column 2 which is the spinner. You would have to set a weight on each of the columns. Something like

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" >
  <TableRow>

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_column="0"
            android:gravity="right"
            android:paddingLeft="3dip"
            android:text="Sold To"
            android:textColor="#000033"
            android:textSize="17sp" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_column="1"
            android:gravity="left"
            android:text="*"
            android:textColor="#FF0000"
            android:textSize="17sp" />

        <Spinner
            android:id="@+id/sold_to_dd"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:layout_column="2"
            android:singleLine="true"
             />
    </TableRow>


</TableLayout>

A weight would indicate the % of the screen to occupy. so each of the textViews would take 20% of the screen and the spinner would take 60%.

You could also refer to this : Android Holo theme does not wrap multiple line spinner dropdown items

Community
  • 1
  • 1
lokoko
  • 5,785
  • 5
  • 35
  • 68