4

I have a horizontal linear layout with multiple text views in it. The layout has a width of fill_parent and height of wrap_content. My issue is that when the text views are longer than the allowed screen width of my device, it smashes the last text view to the right instead of wrapping it to a new line (which is my desired result). I thought having a height of wrap_content would solve this.

What can i do to make it so my linear layout with multiple text views will wrap the text views if they exceed the width of the parent?

Here is a snippet from my layout:

<LinearLayout
                                android:id="@+id/linearLayout20"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:paddingTop="3dip" >

                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:text="Car Info:  " android:id="@+id/textView023" android:layout_width="wrap_content" android:textStyle="bold" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:text="2005" android:id="@+id/autorental_item_caryear" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:paddingLeft="3dip" android:text="Chevy" android:id="@+id/autorental_item_carmake" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:paddingLeft="3dip" android:text="Rio" android:id="@+id/autorental_item_carmodel" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:paddingLeft="3dip" android:text="-" android:id="@+id/textView2" android:paddingRight="3dip" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                                <TextView android:layout_height="wrap_content" android:textColor="@color/black" android:paddingLeft="3dip" android:text="Red" android:id="@+id/autorental_item_carcolor" android:layout_width="wrap_content" android:textSize="@dimen/item_main_text_size"></TextView>
                            </LinearLayout>

With long textviews, it renders something like this:

Car Info:  2002 Chevrolet Silverado - Pu
                                     rp
                                     le 

and I would like it to be something like this:

Car Info:  2002 Chevrolet Silverado - 
Purple
Nick
  • 1,262
  • 1
  • 17
  • 32
  • You want TextView with having equal size width? – Paresh Mayani Apr 16 '12 at 13:06
  • No they don't need to be equal size. They will vary based on what someone inputs later. I just want it so that if the text in the text views becomes so long that it can't fit on one like--it will wrap to the next line. Right now if the text is too long, it smashes the last text view to the right, making it multiple lines long but with only 1-2 characters on each line all smashed to the right – Nick Apr 16 '12 at 13:12

2 Answers2

3

Your desired result is not possible with a LinearLayout containing multiple TextViews (or other things such as Buttons etc.):

Every horizontal/vertical linear layout gives its child views horizontal/vertical boundarys in which each child can do it's own thing. So each view can expand only in the direction which is not bounded, in your case vertical.

Here is a sample which illustrates the problem. The outer dashes represent the screen borders.

| First Text- | Short text | Long text will take all the space possible | T |
| View with   |            |                                            | e |
| additional  |            |                                            | x |
| given       |            |                                            | t |
| maximum     |            |                                            |   |
| width. Long |            |                                            |   |
| text makes  |            |                                            |   |
| it expand   |            |                                            |   | 
| down        |            |                                            |   |

You can solve it by putting all the text into a single textview. Then again you need to format the strings you put inside if you want to make a nice list.

stefan
  • 10,215
  • 4
  • 49
  • 90
1

You can't achieve that result using a LinearLayout . I think the best solution in this case is to use a single TextView with multiple styles in (if you need dynamic values for text). take a look here for insights: Is it possible to have multiple styles inside a TextView?

If you have static text instead, you can enclose an html-compliant value in a CDATA block inside your strings.xml and use it directly in your layout.

Community
  • 1
  • 1
a.bertucci
  • 12,142
  • 2
  • 31
  • 32