2

I have a View comprised of two TextView if first is too wide can I get the second to display on the next line?

Is there a solution?

<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView android:text="If this text is too wide"
android:textSize="20px"
android:textStyle="bold"
android:id="@+id/del01"
android:paddingTop="3px"
android:paddingBottom="3px"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView android:text="I want this on a different line"
android:id="@+id/del02"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
Macarse
  • 91,829
  • 44
  • 175
  • 230
Milk Round
  • 97
  • 7
  • is it a must to have two TextView's? Why not to use one and limit it to two lines? – Asahi Aug 14 '10 at 14:10
  • I would assume it's because of how he wants the wrapping to behave; if it was a single TextView then it would wrap at the limit, but he wants it to wrap on each portion of the text. – Dan Lew Aug 14 '10 at 14:16
  • Excellent question. I have been trying for some minutes and couldn't do it :( – Macarse Aug 14 '10 at 14:32
  • I suspect the answer would be quite complex. There was an old question on SO about wrapping layout that you might have to use: http://stackoverflow.com/questions/549451/line-breaking-widget-layout-for-android – Dan Lew Aug 14 '10 at 16:47
  • Got it!!!! have two views v2a and v2b v1.measure(v1.getWidth(),v1.getHeight()); v2a.measure(v2a.getWidth(),v2a.getHeight()); if (lv.getWidth()< (v1.getMeasuredWidth()+v2a.getMeasuredWidth())) //too big v2a.setVisibility(View.GONE); else v2b.setVisibility(View.GONE); thanks for the thoughts anyway Steve – Milk Round Aug 15 '10 at 08:22

1 Answers1

1

Create your own view class derived from View and display both texts in that view. Override draw() and you can do whatever you want there depending on the sizes of the text.

Note: It is usually not a good idea to define sizes and paddings in "px". You should rather use "dp" which will be automatically scale on different screen sizes and densities.

StefanMK
  • 1,313
  • 1
  • 12
  • 22
  • Got it!!!! have two views v2a and v2b v1.measure(v1.getWidth(),v1.getHeight()); v2a.measure(v2a.getWidth(),v2a.getHeight()); if (lv.getWidth()< (v1.getMeasuredWidth()+v2a.getMeasuredWidth())) //too big v2a.setVisibility(View.GONE); else v2b.setVisibility(View.GONE); thanks for the thoughts anyway Steve – Milk Round Aug 15 '10 at 08:19